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
+1
syedghazi111
This comment was deleted.
0
khemu1 day ago
 bool find3Numbers(int A[], int n, int X)   {       //Your Code Here       sort(A,A+n);       for(int i=0;i<n;i++){               int search = X-A[i];               int low=i+1,high=n-1;               while(low<high){                   if((A[low]+A[high])==search)                       return true;                   else if((A[low]+A[high])>search)                       high--;                   else                       low++;               }       }       return false;   }
0
_shivam_2 days ago
def find3Numbers(self,A, n, X):
        # Your Code Here
        count = 0
        for i in range(0, n-1):
            s = set()
            curr_sum = X - A[i]
            for j in range(i + 1, n):
                if (curr_sum - A[j]) in s:
                   count += 1
                s.add(A[j])
        return count
            

+1
adityamurali22 days ago
bool find3Numbers(int A[], int n, int X)   {       sort(A, A+n);       int res = 0;        for(int i = 0; i < n-2; i++)       {           int left = i+1;           int right = n-1;           while(left < right)           {               if(A[i] + A[left] + A[right] == X)               {                   res = 1;                   break;                                  }               else if(A[i]+A[left]+A[right] < X)                   left++;               else                   right--;           }           if(res == 1)               return res;                  }                         return 0;   }
0
abhinayabhi1414 days ago
class Solution:     #Function to find if there exists a triplet in the   #array A[] which sums up to X.  def find3Numbers(self,A, n, X):      for i in range(0, n-1):          s = set()          curr_sum = X - A[i]          for j in range(i + 1, n):              if (curr_sum - A[j]) in s:                  return True              s.add(A[j])      return False
0
ashivamsahu146 days ago
PYTHON
class Solution:
    
   #Function to find if there exists a triplet in the 
   #array A[] which sums up to X.
   def find3Numbers(self,A, n, X):
       for i in range(0, n-1):
           s = set()
           curr_sum = X - A[i]
           for j in range(i + 1, n):
               if (curr_sum - A[j]) in s:
                   return True
               s.add(A[j])
       return False
0
kirubhakaraguru6 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) {          // Your code Here      boolean ans = false;      Arrays.sort(A);      for(int i=0; i<A.length; i++){          int j = i+1;          int k = n-1;          while(j<k){              if(A[i]+A[j]+A[k]==X){                  ans = true;                  return ans;              }              else if(A[i]+A[j]+A[k] > X){                  k--;              }              else{                  j++;              }          }      }       return ans;   }} 
0
user_srbckq46yj91 week ago
       int a=0;       sort(A,A+n);       for(int i=0;i<n;i++)       {           a=X-A[i];           int *b=&A[i+1];           int *c=&A[n-1];           while(b<c)           {               if(a-*b-*c==0) return true;               else if(a-*b-*c<0)               {                   c--;               }               else                {                   b++;               }           }           a=0;       }       return false;
0
user_srbckq46yj9
This comment was deleted.
0
devesh1g6bi1 week ago
#Python
class Solution:       #Function to find if there exists a triplet in the    #array A[] which sums up to X.   def find3Numbers(self,A, n, X):       for i in range(0, n-1):           s = set()           curr_sum = X - A[i]           for j in range(i + 1, n):               if (curr_sum - A[j]) in s:                   return True               s.add(A[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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.