Count the triplets | Practice | GeeksforGeeks

N = 4
arr[] = {1, 5, 3, 2}
Output: 2
Explanation: There are 2 triplets: 
1 + 2 = 3 and 3 +2 = 5 
â€‹Example 2: 
Input: 
N = 3
arr[] = {2, 3, 4}
Output: 0
Explanation: No such triplet exits
Your Task:  
You don't need to read input or print anything. Your task is to complete the function countTriplet() which takes the array arr[] and N as inputs and returns the triplet count

Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 103
1 ≤ arr[i] ≤ 105
0
kumarbivekshaw9991 hour ago
int j,k,sum=0,count=0,l;           for(j=0;j<n-1;j++)           {               for(k=j+1;k<n;k++)               {                   sum=arr[j]+arr[k];                   for(l=0;l<n;l++)                   {                       if(arr[l]==sum)                       {                           count++;                       }                   }               }           }              return count;
0
rksing43214 days ago
without sorting we can also do this :
 
int countTriplet(int arr[], int n){    // Your code goes here    int count =0;        int id[1000000]={0};        for(int i=0; i<n; i++)        id[arr[i]] = 1;                    for(int i=0; i<n-1; i++)    {        int j=i+1;        while(j<n)        {            if(id[arr[i] + arr[j]] == 1)                count++;            j++;        }    }    return count;
}
0
sy9924556 days ago
 int countTriplet(int arr[], int n) {
        HashSet<Integer> set = new HashSet<>();
        for(int val : arr){
            set.add(val);
        }
        int count = 0;
        for(int i=0;i<n;i++){
            for(int j = i+1;j<n;j++){
                int sum =arr[i]+arr[j];
                if(set.contains(sum)){
                    count++;
                }
            }
        }
        return count;
    }
0
pranjalp711 week ago
Java using two pointer in n^2 time complexity 
class Solution {   int countTriplet(int arr[], int n) {       // code here       Arrays.sort(arr);       int count = 0;              for(int i=0;i<n;i++){           int target = arr[i];           int s = 0,  e = n-1;  //s=pointer at start and e = pointer at end          while(s<e) {                         if(arr[s]+arr[e] == target){               count++;s++;e--;               //System.out.print(arr[s]+" "+arr[e]+"   ");               //break;           }           else if(arr[s]+arr[e] > target)e--;           else s++;          }        }       return count;   }}
0
sarkarchitra67Premium1 week ago
class Solution{
public:	
	
	int countTriplet(int arr[], int n)
	{
	    // Your code goes here
	    
	    unordered_map<int, int> m;
    for(int i=0; i<n; i++){
        m[arr[i]]++;
    }
	    int an=0;
	    for(int i =0 ;i<n-1;i++){
	        for(int j=i+1;j<n;j++){
	            if(m[arr[i]+arr[j]]!=0){
	                an++;
	            }
	        }
	    }
	    return an;
	    
	}
};
0
nagasivakrishnav2 weeks ago
Python code:
used dicts instead of arrays. takes some space but time complexity is restricted to O(n^2). 
	   tripls = 0
       hashmap = {}
       for i in range(n):
           hashmap[arr[i]] = 1
           # since all numbers are distinct give any frequency. It doesn't matter.
       for i in range(n):
           for j in range(i+1,n):
               if arr[i] + arr[j] in hashmap.keys():
                   tripls += 1
       return tripls
0
arnabkunduary96
This comment was deleted.
0
nehasharma3
This comment was deleted.
0
mitradiptamoy3 weeks ago
//using two pointer technique
 
int countTriplet(int arr[], int n){    // Your code goes here    sort(arr,arr+n);    int count=0;    for(int i=n-1;i>=2;i--)    {        int l=0;        int h=i-1;        while(l<h)        {            if(arr[l]+arr[h]==arr[i])            {                count++;                l++;                h--;            }            else if(arr[l]+arr[h]<arr[i])            {                l++;            }            else            {                h--;            }        }    }    return count;}
0
mohammadtarique6619981 month ago
N = int(input())arr = []for i in range(0,N):   a = int(input())   arr.append(a)print(arr)count = 0for i in arr:   for j in arr:       if (i != j) and ((i+j in arr)) and arr.index(i)<arr.index(j):           count = count+1
print(count)
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.