Find four elements that sum to a given value (4Sum) | Set 1 (n^3 solution)
Last Updated :
11 Sep, 2023
Given an array of integers, find all combination of four elements in the array whose sum is equal to a given value X.
Example:
Input: array = {10, 2, 3, 4, 5, 9, 7, 8}, X = 23
Output: 3 5 7 8
Explanation: Sum of output is equal to 23, i.e. 3 + 5 + 7 + 8 = 23.
Input: array = {1, 2, 3, 4, 5, 9, 7, 8}, X = 16
Output: 1 3 5 7
Explanation: Sum of output is equal to 16, i.e. 1 + 3 + 5 + 7 = 16.
A Naive Solution is to generate all possible quadruples and compare the sum of every quadruple with X. The following code implements this simple method using four nested loops
C++
#include <bits/stdc++.h>
using namespace std;
void findFourElements( int A[], int n, int X)
{
for ( int i = 0; i < n - 3; i++)
{
for ( int j = i + 1; j < n - 2; j++)
{
for ( int k = j + 1; k < n - 1; k++)
{
for ( int l = k + 1; l < n; l++)
if (A[i] + A[j] + A[k] + A[l] == X)
cout << A[i] << ", " << A[j]
<< ", " << A[k] << ", " << A[l];
}
}
}
}
int main()
{
int A[] = {10, 20, 30, 40, 1, 2};
int n = sizeof (A) / sizeof (A[0]);
int X = 91;
findFourElements (A, n, X);
return 0;
}
|
C
#include <stdio.h>
void findFourElements( int A[], int n, int X)
{
for ( int i = 0; i < n-3; i++)
{
for ( int j = i+1; j < n-2; j++)
{
for ( int k = j+1; k < n-1; k++)
{
for ( int l = k+1; l < n; l++)
if (A[i] + A[j] + A[k] + A[l] == X)
printf ( "%d, %d, %d, %d" , A[i], A[j], A[k], A[l]);
}
}
}
}
int main()
{
int A[] = {10, 20, 30, 40, 1, 2};
int n = sizeof (A) / sizeof (A[0]);
int X = 91;
findFourElements (A, n, X);
return 0;
}
|
Java
class FindFourElements
{
void findFourElements( int A[], int n, int X)
{
for ( int i = 0 ; i < n - 3 ; i++)
{
for ( int j = i + 1 ; j < n - 2 ; j++)
{
for ( int k = j + 1 ; k < n - 1 ; k++)
{
for ( int l = k + 1 ; l < n; l++)
{
if (A[i] + A[j] + A[k] + A[l] == X)
System.out.print(A[i]+ " " +A[j]+ " " +A[k]
+ " " +A[l]);
}
}
}
}
}
public static void main(String[] args)
{
FindFourElements findfour = new FindFourElements();
int A[] = { 10 , 20 , 30 , 40 , 1 , 2 };
int n = A.length;
int X = 91 ;
findfour.findFourElements(A, n, X);
}
}
|
Python3
def findFourElements(A, n, X):
for i in range ( 0 ,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:
print ( "%d, %d, %d, %d"
% ( A[i], A[j], A[k], A[l]))
A = [ 10 , 2 , 3 , 4 , 5 , 9 , 7 , 8 ]
n = len (A)
X = 23
findFourElements (A, n, X)
|
C#
using System;
class FindFourElements
{
void findFourElements( int []A, int n, int X)
{
for ( int i = 0; i < n - 3; i++)
{
for ( int j = i + 1; j < n - 2; j++)
{
for ( int k = j + 1; k < n - 1; k++)
{
for ( int l = k + 1; l < n; l++)
{
if (A[i] + A[j] + A[k] + A[l] == X)
Console.Write(A[i] + " " + A[j] +
" " + A[k] + " " + A[l]);
}
}
}
}
}
public static void Main()
{
FindFourElements findfour = new FindFourElements();
int []A = {10, 20, 30, 40, 1, 2};
int n = A.Length;
int X = 91;
findfour.findFourElements(A, n, X);
}
}
|
Javascript
<script>
function findFourElements(A, n, X)
{
for (let i = 0; i < n - 3; i++)
{
for (let j = i + 1; j < n - 2; j++)
{
for (let k = j + 1; k < n - 1; k++)
{
for (let l = k + 1; l < n; l++)
if (A[i] + A[j] + A[k] + A[l] == X)
document.write(A[i]+ ", " +A[j]+ ", " +A[k] + ", " +A[l]);
}
}
}
}
let A = [10, 20, 30, 40, 1, 2];
let n = A.length;
let X = 91;
findFourElements (A, n, X);
</script>
|
PHP
<?php
function findFourElements( $A , $n , $X )
{
for ( $i = 0; $i < $n -3; $i ++)
{
for ( $j = $i +1; $j < $n -2; $j ++)
{
for ( $k = $j +1; $k < $n -1; $k ++)
{
for ( $l = $k +1; $l < $n ; $l ++)
if ( $A [ $i ] + $A [ $j ] +
$A [ $k ] + $A [ $l ] == $X )
echo $A [ $i ], ", " , $A [ $j ],
", " , $A [ $k ], ", " , $A [ $l ];
}
}
}
}
$A = array (10, 20, 30, 40, 1, 2);
$n = sizeof( $A ) ;
$X = 91;
findFourElements ( $A , $n , $X );
?>
|
Time Complexity: O(n^4)
Auxiliary Space: O(1)
The time complexity can be improved to O(n^3) with the use of sorting as a preprocessing step, and then using method 1 of this post to reduce a loop.
- Sort the input array.
- Fix the first element as A[i] where i is from 0 to n–3. After fixing the first element of quadruple, fix the second element as A[j] where j varies from i+1 to n-2. Find remaining two elements in O(n) time, using the method 1 of this post
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int compare( const void * a, const void * b)
{
return (*( int *)a - *( int *)b);
}
void find4Numbers( int A[], int n, int X)
{
int l, r;
qsort (A, n, sizeof (A[0]), compare);
for ( int i = 0; i < n - 3; i++) {
for ( int j = i + 1; j < n - 2; j++) {
l = j + 1;
r = n - 1;
while (l < r) {
if (A[i] + A[j] + A[l] + A[r] == X) {
cout << A[i] << ", " << A[j] << ", "
<< A[l] << ", " << A[r];
l++;
r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else
r--;
}
}
}
}
int main()
{
int A[] = { 1, 4, 45, 6, 10, 12 };
int X = 21;
int n = sizeof (A) / sizeof (A[0]);
find4Numbers(A, n, X);
return 0;
}
|
C
# include <stdio.h>
# include <stdlib.h>
int compare ( const void *a, const void * b)
{ return ( *( int *)a - *( int *)b ); }
void find4Numbers( int A[], int n, int X)
{
int l, r;
qsort (A, n, sizeof (A[0]), compare);
for ( int i = 0; i < n - 3; i++)
{
for ( int j = i+1; j < n - 2; j++)
{
l = j + 1;
r = n-1;
while (l < r)
{
if ( A[i] + A[j] + A[l] + A[r] == X)
{
printf ( "%d, %d, %d, %d" , A[i], A[j],
A[l], A[r]);
l++; r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else
r--;
}
}
}
}
int main()
{
int A[] = {1, 4, 45, 6, 10, 12};
int X = 21;
int n = sizeof (A)/ sizeof (A[0]);
find4Numbers(A, n, X);
return 0;
}
|
Java
import java.util.Arrays;
class FindFourElements
{
void find4Numbers( int A[], int n, int X)
{
int l, r;
Arrays.sort(A);
for ( int i = 0 ; i < n - 3 ; i++)
{
for ( int j = i + 1 ; j < n - 2 ; j++)
{
l = j + 1 ;
r = n - 1 ;
while (l < r)
{
if (A[i] + A[j] + A[l] + A[r] == X)
{
System.out.println(A[i]+ " " +A[j]+ " " +A[l]+ " " +A[r]);
l++;
r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else
r--;
}
}
}
}
public static void main(String[] args)
{
FindFourElements findfour = new FindFourElements();
int A[] = { 1 , 4 , 45 , 6 , 10 , 12 };
int n = A.length;
int X = 21 ;
findfour.find4Numbers(A, n, X);
}
}
|
Python 3
def find4Numbers(A, n, X):
A.sort()
for i in range (n - 3 ):
for j in range (i + 1 , n - 2 ):
l = j + 1
r = n - 1
while (l < r):
if (A[i] + A[j] + A[l] + A[r] = = X):
print (A[i], "," , A[j], "," ,
A[l], "," , A[r])
l + = 1
r - = 1
elif (A[i] + A[j] + A[l] + A[r] < X):
l + = 1
else :
r - = 1
if __name__ = = "__main__" :
A = [ 1 , 4 , 45 , 6 , 10 , 12 ]
X = 21
n = len (A)
find4Numbers(A, n, X)
|
C#
using System;
class FindFourElements
{
void find4Numbers( int []A, int n, int X)
{
int l, r;
Array.Sort(A);
for ( int i = 0; i < n - 3; i++)
{
for ( int j = i + 1; j < n - 2; j++)
{
l = j + 1;
r = n - 1;
while (l < r)
{
if (A[i] + A[j] + A[l] + A[r] == X)
{
Console.Write(A[i] + " " + A[j] +
" " + A[l] + " " + A[r]);
l++;
r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else
r--;
}
}
}
}
public static void Main()
{
FindFourElements findfour = new FindFourElements();
int []A = {1, 4, 45, 6, 10, 12};
int n = A.Length;
int X = 21;
findfour.find4Numbers(A, n, X);
}
}
|
Javascript
<script>
function find4Numbers(A,n,X)
{
let l, r;
A.sort( function (a, b){ return a - b});
for (let i = 0; i < n - 3; i++)
{
for (let j = i + 1; j < n - 2; j++)
{
l = j + 1;
r = n - 1;
while (l < r)
{
if (A[i] + A[j] + A[l] + A[r] == X)
{
document.write(A[i]+ " " +A[j]+ " " +A[l]+ " " +A[r]+ "<br>" );
l++;
r--;
}
else if ((A[i] + A[j] + A[l] + A[r]) < X)
{
l++;
}
else
{
r--;
}
}
}
}
}
let A= [1, 4, 45, 6, 10, 12];
let n = A.length;
let X = 21;
find4Numbers(A, n, X)
</script>
|
PHP
<?php
function find4Numbers( $A , $n , $X )
{
$l ; $r ;
sort( $A );
for ( $i = 0; $i < $n - 3; $i ++)
{
for ( $j = $i + 1; $j < $n - 2; $j ++)
{
$l = $j + 1;
$r = $n - 1;
while ( $l < $r )
{
if ( $A [ $i ] + $A [ $j ] +
$A [ $l ] + $A [ $r ] == $X )
{
echo $A [ $i ] , " " , $A [ $j ] ,
" " , $A [ $l ] ,
" " , $A [ $r ];
$l ++;
$r --;
}
else if ( $A [ $i ] + $A [ $j ] +
$A [ $l ] + $A [ $r ] < $X )
$l ++;
else
$r --;
}
}
}
}
$A = array (1, 4, 45, 6, 10, 12);
$n = count ( $A );
$X = 21;
find4Numbers( $A , $n , $X );
?>
|
Time Complexity : O(n^3)
Auxiliary Space: O(1)
This problem can also be solved in O(n^2Logn) complexity. Please refer below post for details
Find four elements that sum to a given value | Set 2 ( O(n^2Logn) Solution)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...