Triplet Sum in Array | Practice | GeeksforGeeks

Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. 

Example 1:
Input:
n = 6, X = 13
arr[] = [1 4 45 6 10 8]
Output:
1
Explanation:
The triplet {1, 4, 8} in 
the array sums up to 13.
Example 2:
Input:
n = 5, X = 10
arr[] = [1 2 4 3 6]
Output:
1
Explanation:
The triplet {1, 3, 6} in 
the array sums up to 10.


Your Task:
You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise.

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

Constraints:
1 ≤ n ≤ 103
1 ≤ A[i] ≤ 105
0
sy9924554 days ago
       //Sort the array
        Arrays.sort(arr);
        for(int i = 0;i <= n-3; i++){
            int target = x - arr[i];
            int j = i + 1;
            int k = n -1;
            
            while(j < k){
                int sum = arr[j] + arr[k];
                if(target == sum) return true;
                else if(target > sum) j++;
                else k--;
            }
        }
        return false;
    }
0
janvijindal055 days ago
 sort(A,A+n);      for(int i=0;i<n;i++){          int y=X-A[i];          int low=i+1;          int high=n-1;          while(low<high){        if(A[low]+A[high]==y){            return 1;        }        else if(A[low]+A[high]>y){            high--;        }       else if(A[low]+A[high]<y){           low++;       }          }      }       return 0;   } 
0
mayurxxivk5 days ago
CorrectAnswer. 🍁
Total Time Taken:0.2/1.2
 
Do not use extra space just sort it first and apply two pointer /three pointer approach
 public static boolean find3Numbers(int arr[], int n, int k) { 
    
       Arrays.sort(arr);
       int cv = 0;                 // current value
       while(cv < n-2)
       {
            int l = cv+1 ;
            int h = n-1;
            
            while(l<h)
          {
           
           if(arr[l] + arr[h] + arr[cv] == k)
           return true;
           
           if(arr[l] + arr[h] + arr[cv] > k)
           h--;
           
           if(arr[l] + arr[h] + arr[cv] < k)
           l++;
           
         }
            cv++;
         }    
    return false;
        
    }
   
0
pratiksehjpal20006 days ago
class Solution
{
    //Function to find if there exists a triplet in the 
    //array A[] which sums up to X.
    public static boolean find3Numbers(int A[], int n, int X) { 
      HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
      for(int i=0;i<n;i++)
      {
          map.put(A[i],1);
      }
      for(int i=0;i<n-1;i++)
      {
          for(int j=i+1;j<n;j++)
          {
              int sum = A[i]+A[j];
              if(map.containsKey(X-sum)&&X-sum!=A[i]&&X-sum!=A[j])
              {
                  return true;
              }
          }
      }
    return false;
    }
}
0
sowmyadevineni6001 week ago
input: 8 45 6 10 1 4
output : 1
expected output : 0
why??
i did'nt get why i am getting wrong output even after solving it correctly
int sum;       set<int> mp;       for(int i=0;i<n;i++)       {           mp.insert(A[i]);       }       for(int i=0;i<n-1;i++)       {           for(int j=i+1;j<n-1;j++)           {               sum=A[i]+A[j];               if(sum<X)               {                   if(mp.find(X-sum)!=mp.end())                   {                       return true;                   }               }               else                  continue;           }       }       return false;   }
+1
spideyy1 week ago
bool find3Numbers(int A[], int n, int X)   {       unordered_set<int>s;       for(int i=0;i<n;i++) s.insert(A[i]);       for(int i=0;i<n;i++){           for(int j=i+1;j<n-1;j++){               int ans = A[i]+A[j];               if(s.find(X-ans)!=s.end()){                   return 1;               }           }       }       return 0;   }
0
ajay7yadav951 week ago
// Hello JavaScript lovers -------------------→ A7
for(let i=0; i<arr.length; i++){
    let k = i;
    let l = i+1;
    let r = arr.length-1;
   
    while(l<r){
        if(arr[k]+arr[l]+arr[r]==key){
                 return key;
        }
        if(arr[k]+arr[l]+arr[r]<key){
            l++;
        }else{
            r--;
        }
    }
}
 
+2
hharshit81181 week ago
    bool find3Numbers(int arr[], int n, int X)   {       sort(arr, arr+n);         for(int i=0; i< n; i++){       int low=i+1, high=n-1;       while(low<high){           int sum = arr[low]+arr[high]+arr[i];           if(sum == X){               return true;           }           if(sum < X){               low++;           }           else{               high--;           }       }   }
0
ashutos17sharma89891 week ago
JAVA SOLUTION
class Solution
{
    //Function to find if there exists a triplet in the 
    //array A[] which sums up to X.
    public static boolean find3Numbers(int A[], int n, int X) { 
    
       // Your code Here
       Arrays.sort(A);
       for(int i =0;i<n-2;i++){
           int a= A[i];
           int newX=X-a;
           int l=i+1;
           int h=n-1;
           while(l<h){
               if(A[l]+A[h]==newX){
                   return true;
               }else if(A[l]+A[h]>newX){
                   h--;
               }else{
                   l++;
               }
           }
       }
       return false;
    
    }
}
 
+1
2019sushilkumarkori1 week ago
bool find3Numbers(int A[], int n, int X)
    {
        //Your Code Here
        sort(A,A+n);
        for(int i=0;i<n-1;i++){
            int j=i+1,k=n-1;
            while(k>j){
                int sum=A[i]+A[j]+A[k];
                if(sum == X){
                    return true;
                }
                else if(sum>X){
                    k--;
                }
                else{
                    j++;
                }
            }
        }
        return false;
    }

};
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.