`{"id":"6efaeba6-d5a1-4770-a3be-d07a07482146","name":"Count The Triplets","description":"Given an array of distinct integers. The task is to count all the triplets such that sum of two elements equals the third element.\r\n","inputFormat":"Input is managed for you\r\n","outputFormat":"Output is managed for you\r\n","constraints":"1 &lt;= N &lt;= 10^3\r\n1 &lt;= arr[i] &lt;= 10^5\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n int countTriplets(vector<int> &arr) {\n //write your code here\n \n }\n\n int main() {\n //input work\n int n;\n cin>>n;\n vector<int> arr(n);\n\n for (int i = 0; i < n; i++) {\n int data;\n cin>>data;\n arr[i] = data;\n }\n\n cout<<countTriplets(arr);\n }"},"java":{"code":"import java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n public static int countTriplets(int[]arr) {\r\n //write your code here\r\n }\r\n\r\n public static void main(String[]args) {\r\n //input work\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n int[]arr = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n System.out.println(countTriplets(arr));\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n1 5 3 2","sampleOutput":"2","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"a46023c2-500c-4e7e-8470-3de93eeafcd7","name":"Count The Triplets","slug":"count-the-triplets","type":1}],"next":{"id":"ca6a2331-c2ef-43d4-8715-7575039b9149","name":"Count The Triplets","type":3,"slug":"count-the-triplets"},"prev":{"id":"086d33b4-a97c-4082-9998-53c1e813c327","name":"Allocate Minimum Number Of Pages MCQ","type":0,"slug":"allocate-minimum-number-of-pages-mcq"}}}`

# Count The Triplets

Given an array of distinct integers. The task is to count all the triplets such that sum of two elements equals the third element.

`{"id":"6efaeba6-d5a1-4770-a3be-d07a07482146","name":"Count The Triplets","description":"Given an array of distinct integers. The task is to count all the triplets such that sum of two elements equals the third element.\r\n","inputFormat":"Input is managed for you\r\n","outputFormat":"Output is managed for you\r\n","constraints":"1 &lt;= N &lt;= 10^3\r\n1 &lt;= arr[i] &lt;= 10^5\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n int countTriplets(vector<int> &arr) {\n //write your code here\n \n }\n\n int main() {\n //input work\n int n;\n cin>>n;\n vector<int> arr(n);\n\n for (int i = 0; i < n; i++) {\n int data;\n cin>>data;\n arr[i] = data;\n }\n\n cout<<countTriplets(arr);\n }"},"java":{"code":"import java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n public static int countTriplets(int[]arr) {\r\n //write your code here\r\n }\r\n\r\n public static void main(String[]args) {\r\n //input work\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n int[]arr = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n System.out.println(countTriplets(arr));\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n1 5 3 2","sampleOutput":"2","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"a46023c2-500c-4e7e-8470-3de93eeafcd7","name":"Count The Triplets","slug":"count-the-triplets","type":1}],"next":{"id":"ca6a2331-c2ef-43d4-8715-7575039b9149","name":"Count The Triplets","type":3,"slug":"count-the-triplets"},"prev":{"id":"086d33b4-a97c-4082-9998-53c1e813c327","name":"Allocate Minimum Number Of Pages MCQ","type":0,"slug":"allocate-minimum-number-of-pages-mcq"}}}`

Editor

Loading...

# Count The Triplets

easy

Given an array of distinct integers. The task is to count all the triplets such that sum of two elements equals the third element.

## Constraints

1 <= N <= 10^3 1 <= arr[i] <= 10^5

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 1 5 3 2```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}2`

Discussions

Show Discussion

Related Resources