Find triplets with zero sum | Practice | GeeksforGeeks

Given an array arr[] of n integers. Check whether it contains a triplet that sums up to zero. 
Example 1:
Input: n = 5, arr[] = {0, -1, 2, -3, 1}
Output: 1
Explanation: 0, -1 and 1 forms a triplet
with sum equal to 0.
Example 2:
Input: n = 3, arr[] = {1, 2, 3}
Output: 0
Explanation: No triplet with zero sum exists. 

Your Task:
You don't need to read input or print anything. Your task is to complete the boolean function findTriplets() which takes the array arr[] and the size of the array (n) as inputs and print 1 if the function returns true else print 0 if the function returns false. 

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

Constrains:
1 <= n <= 104
-106 <= Ai <= 106
-2
bsyed1762 days ago
JAVA solution
class Solution{public boolean findTriplets(int arr[] , int n)   {       //add code here.             // Arrays.sort(arr);       for(int i=0;i<n-2;i++){           for(int j=i+1;j<n-1;j++){             for(int k=j+1;k<n;k++){                  int s=(arr[i]+arr[j]+arr[k]);                       if(s==0){                    return true;                   }             }        }     }return false;   }}
-2
kalaiabster1 week ago
JAVA SOLUTION  Time taken(1.15/2.73):
 
public boolean findTriplets(int arr[] , int n)   {       for(int i=0;i<n-2;i++)       {           for(int j=i+1;j<n-1;j++)           {               for(int k=j+1;k<n;k++)               {                   if(arr[i]+arr[j]+arr[k]==0)                   return true;               }           }       }       return false;   }
0
harshscode1 week ago
 sort(a,a+n);
        
        for(int i=0;i<n-2;i++)
        {
            int j=i+1,k=n-1;
            while(j<k)
            {
               
                if(a[i]+a[j]+a[k]==0)
                return true;
                else if( a[i]+a[j]+a[k]<0)
                j++;
                else
                k--;
            }
        }
         return false;
        
0
abhilaygude1 week ago
from itertools import combinationsclass Solution:       def findTriplets(self, arr, n):       c=combinations(arr,3)       for i in c:           if sum(i)==0:               return 1       else:           return 0
-1
hharshit81182 weeks ago
  bool findTriplets(int arr[], int n)   {    sort(arr, arr+n);       for(int i=0; i<n-2; i++){           int low =i+1, high =n-1;           while(low < high){               int sum = arr[i] + arr[low] + arr[high];               if(sum == 0){                   return true;               }               if(sum < 0){                   low++;               }               else{                   high--;               }           }       }       return false;   }
0
ashtosh20012 weeks ago
class Solution{public boolean findTriplets(int arr[] , int n)   {       Arrays.sort(arr);       for(int i=0;i<n;i++){           int low=i+1 , high = n-1;                      while(low<high){               int sum = arr[i]+arr[low] + arr[high];               if(sum==0){                   return true;               }               else if(sum>0){                   high--;               }else{                   low++;               }           }       }       return false;   }}
+3
shubham211019972 weeks ago
why gfg consider this as basic question it must be under easy to medium level;
0
shubham211019972 weeks ago
bool findPair(int arr[],int si,int li,int X){       int i=si,j=li;       while(i<j){           int sum=arr[i]+arr[j];           if(sum==X) return true;           else if(sum<X)i++;           else j--;       }       return false;   }   bool findTriplets(int arr[], int n)   {        sort(arr,arr+n);       //pair        for(int i=0;i<=n-2;i++){           if(findPair(arr,i+1,n-1,-arr[i]))             return true;       }       return false;   }
0
mestryruturaj3 weeks ago
//Java
class Solution
{
    // arr[]: input array
    // n: size of the array
    //Function to find triplets with zero sum.
	public boolean findTriplets(int arr[] , int n)
    {   
        
        //Time Complexity = O(n*n);
        Arrays.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            int start = i+1;
            int end = arr.length - 1;
            
            while (start < end) {
                if (arr[i] + arr[start] + arr[end] == 0) {
                    return true;
                }
                else if (arr[i] + arr[start] + arr[end] < 0) {
                    start++;
                }
                else {
                    end--;
                }
            }
        }
        return false;
        /*
        //Brute Force Approach
        for (int i = 0; i < arr.length - 2; i++) {
            for (int j = i + 1; j < arr.length - 1; j++) {
                for (int k = j + 1; k < arr.length; k++) {
                    if (arr[i] + arr[j] + arr[k] == 0) return true;
                }
            }
        }
        return false;
        */    
    }
}
-1
zerefkhan3 weeks ago
C++ code
Time taken : 0.13s
 
Note : Sorting an array is a terrible idea, it is useless in this case as we have to go scan the entire array anyways. 
class Solution{
  public:
    //Function to find triplets with zero sum.
    bool findTriplets(int arr[], int n)
    {
        if (n >= 3){
            for(int i=0; i<n;i++){
                for(int j=i+1; j<n;j++){
                    for(int k=j+1; k<n;k++){
                        if(arr[i]+arr[j]+arr[k] == 0) return true;
                    }
                }
            }
        }
        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.