Four Elements | Practice | GeeksforGeeks

Given an array A of N integers. You have to find whether a combination of four elements in the array whose sum is equal to a given value X exists or not.
 
Example 1:
Input:
N = 6
A[] = {1, 5, 1, 0, 6, 0}
X = 7
Output:
1

Explantion:
1, 5, 1, 0 are the four elements which makes sum 7.
 

Your Task:  
You don't need to read input or print anything. Your task is to complete the function find4Numbers() which takes the array A[], its size N and an integer X as inputs and returns true if combination is found else false. Driver code will print 1 or 0 accordingly.
 
Expected Time Complexity: O(N3)
Expected Auxiliary Space: O(1)
 
Constraints:
4 <= N <= 100
1 <= A[i] <= 1000
+1
cs190951 month ago
bool find4Numbers(int A[], int n, int X)  
{
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            for(int k=j+1;k<n;k++)
            {
                for(int l=k+1;l<n;l++)
                {
                    if(A[i]+A[j]+A[k]+A[l]==X)
                    {
                        return 1;
                    }
                }
            }
        }
    }
    return 0;
}
+2
gaurabhkumarjha271020013 months ago
bool find4Numbers(int nums[], int n, int target)  
{
    sort (nums, nums+n);
    
  for (int i=0; i< n; i++){
         
    for (int j= i+1; j< n; j++){
             
      int new_target= target - nums[i] - nums[j];
             int k= n-1;
             int l= j+1;
             
       while (l< k){
                 
       int sum= nums[l]+nums[k];
            
         if (sum > new_target)
            k--;
            
     else if (sum  < new_target)
           l++;
                          
            else{
                    
                return true; 
                } 
             }
         }
     }
     
      return false;
}
0
amiransarimy3 months ago
Python Solutions:
Total Time Taken: 0.2/2.4
 
def find4Numbers( A, n, X):
   A.sort() #sort Array
   for i in range(n-3):
       for j in range(i+1,n-2):
           l=j+1
           r=n-1
           while l < r:
               curr_sum = A[i] +A[j] + A[l] + A[r]       
               if curr_sum == X:
                   return True
               elif curr_sum < X:
                   l += 1
               else:
                   r -= 1
   return False
 
0
kashifraza12084 months ago
bool find4Numbers(int arr[], int n, int X)  {       sort(arr , arr+n);
       
       for(int i=0;i<n-3;i++){           for(int j=i+1;j<n-2;j++){               int front = j+1;               int back =  n-1;               while(front<back){                      int sum = arr[i]+arr[j]+ arr[front]+arr[back];                    if(sum<X)                   front++;                   else if(sum>X)                   back--;                   else{                   return true;                   }                                  }           }       }       return false;}
0
rhythmanand6Premium6 months ago
Java Solution 
boolean find4Numbers(int A[], int n, int X) 
    {
        int i, j, l, h;
        Arrays.sort(A);
        for(i=0; i<n-3; i++){
            for(j=i+1; j<n-2; j++){
                l = j+1;
                h = n-1;
                while(l<h){
                    int sum = A[i]+A[j]+A[l]+A[h];
                    if(sum == X) return true;
                    else if(sum<X) l++;
                    else h--;
                }
            }
        }
        return false;
    }
+1
ankitsinha1112006 months ago
bool find4Numbers(int a[], int n, int X)  
{
    int i,j,l,h;
    sort(a,a+n);
    for(i=0;i<n-3;i++)
    {
        for(j=i+1;j<n-2;j++)
        {
            l=j+1;
            h=n-1;
            int sum=a[i]+a[j]+a[l]+a[h];
            while(l<h)
            {
               if(sum==X)
               return 1;
               if(sum<X)
               {
                   sum-=a[l];
               l++;
                sum+=a[l];
               }
               else
               {
                   sum-=a[h];
                    h--;
                sum+=a[h];
               }
            }
        }
    }
    return 0;
}
0
19eucs076zap6 months ago
class Compute{   boolean find4Numbers(int A[], int n, int X)    {       int l,h,req,sum;       Arrays.sort(A);       for(int i=0;i<n-3;i++){           for(int j=i+1;j<n-2;j++){               req=X-(A[i]+A[j]);               l=j+1;               h=n-1;               while(l<h){                   sum=A[l]+A[h];                   if(sum==req){                       return true;                   }                   if(sum<req){                       l++;                   }                   else{                       h--;                   }               }           }       }       return false;   }}
0
sanketsupekar6 months ago
Java Solution 0.3 Sec
class Compute
{
    boolean find4Numbers(int A[], int n, int X) 
    {
        Arrays.sort(A);
        int sum=0;
        for(int i=0;i<n-3;i++)
        {
            for(int j=i+1;j<n-2;j++)
            {
                sum=A[i]+A[j];
                if(sum>=X)
                continue;
                int l=j+1,r=n-1;
                int diff=X-sum;
                while(l<r)
                {
                    int curSum=A[l]+A[r];
                    if(curSum==diff)
                    return true;
                    else if(curSum<diff)
                    l++;
                    else
                    r--;
                }
            }
        }
        return false;
    }
}
0
mehteshamulhaq6 months ago
How to reduce Time Complexity can any one Guide me?
 here Is my code
def find4Numbers( A, n, X):   A.sort()   if len(A) <4 and len(A) >1000:       return False   for i  in range(n-3):       for  j  in range(i+1,n-2):           for k in range(j+1,n-1):               for l in range(k+1,n):                   if A[i]+A[j]+A[k]+A[l] == X:                       return True   else:       return False
-1
shiva10906 months ago
i have used sliding window technique, but the answer is getting wrong for some test cases please find where there is mistake in code.
 
 
bool find4Numbers(int A[], int n, int X)  {      int sum=0;      for(int i=0; i<4; i++){       sum+=A[i];   }      if(sum==X){        return true;          }      for(int i=4; i<n; i++){   if(sum==X){        return true;       break;   }   sum-=A[i-4];   sum+=A[i];   }   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.