Find four elements that sum to a given value | Set 1 (n^3 solution)
Given an array of integers, find all combination of four elements in the array whose sum is equal to a given value X.
For example, if the given array is {10, 2, 3, 4, 5, 9, 7, 8} and X = 23, then your function should print “3 5 7 8” (3 + 5 + 7 + 8 = 23).
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++
// C++ program for naive solution to // print all combination of 4 elements // in A[] with sum equal to X #include <bits/stdc++.h> using namespace std; /* A naive solution to print all combination of 4 elements in A[]with sum equal to X */ void findFourElements( int A[], int n, int X) { // Fix the first element and find other three for ( int i = 0; i < n - 3; i++) { // Fix the second element and find other two for ( int j = i + 1; j < n - 2; j++) { // Fix the third element and find the fourth for ( int k = j + 1; k < n - 1; k++) { // find the fourth 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]; } } } } // Driver Code 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; } // This code is contributed // by Akanksha Rai |
C
// C implementation of above approach #include <stdio.h> /* A naive solution to print all combination of 4 elements in A[] with sum equal to X */ void findFourElements( int A[], int n, int X) { // Fix the first element and find other three for ( int i = 0; i < n-3; i++) { // Fix the second element and find other two for ( int j = i+1; j < n-2; j++) { // Fix the third element and find the fourth for ( int k = j+1; k < n-1; k++) { // find the fourth 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]); } } } } // Driver program to test above function 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
// Java implementation of above approach class FindFourElements { /* A naive solution to print all combination of 4 elements in A[] with sum equal to X */ void findFourElements( int A[], int n, int X) { // Fix the first element and find other three for ( int i = 0 ; i < n - 3 ; i++) { // Fix the second element and find other two for ( int j = i + 1 ; j < n - 2 ; j++) { // Fix the third element and find the fourth for ( int k = j + 1 ; k < n - 1 ; k++) { // find the fourth 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]); } } } } } // Driver program to test above functions 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
#Python 3 implementation of above approach # A naive solution to print all combination # of 4 elements in A[] with sum equal to X def findFourElements(A, n, X): # Fix the first element and find # other three for i in range ( 0 ,n - 3 ): # Fix the second element and # find other two for j in range (i + 1 ,n - 2 ): # Fix the third element # and find the fourth for k in range (j + 1 ,n - 1 ): # find the fourth 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])) # Driver program to test above function A = [ 10 , 2 , 3 , 4 , 5 , 9 , 7 , 8 ] n = len (A) X = 23 findFourElements (A, n, X) # This code is contributed by shreyanshi_arun |
C#
// C# program for naive solution to // print all combination of 4 elements // in A[] with sum equal to X using System; class FindFourElements { void findFourElements( int []A, int n, int X) { // Fix the first element and find other three for ( int i = 0; i < n - 3; i++) { // Fix the second element and find other two for ( int j = i + 1; j < n - 2; j++) { // Fix the third element and find the fourth for ( int k = j + 1; k < n - 1; k++) { // find the fourth 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]); } } } } } // Driver program to test above functions 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); } } // This code is contributed by nitin mittal |
PHP
<?php // Php implementation of above approach /* A naive solution to print all combination of 4 elements in A[] with sum equal to X */ function findFourElements( $A , $n , $X ) { // Fix the first element and find other // three for ( $i = 0; $i < $n -3; $i ++) { // Fix the second element and find // other two for ( $j = $i +1; $j < $n -2; $j ++) { // Fix the third element and // find the fourth for ( $k = $j +1; $k < $n -1; $k ++) { // find the fourth 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 ]; } } } } // Driver program to test above function $A = array (10, 20, 30, 40, 1, 2); $n = sizeof( $A ) ; $X = 91; findFourElements ( $A , $n , $X ); // This code is contributed by m_kit ?> |
Javascript
<script> // Javascript program for the above approach /* A naive solution to print all combination of 4 elements in A[]with sum equal to X */ function findFourElements(A, n, X) { // Fix the first element and find other three for (let i = 0; i < n - 3; i++) { // Fix the second element and find other two for (let j = i + 1; j < n - 2; j++) { // Fix the third element and find the fourth for (let k = j + 1; k < n - 1; k++) { // find the fourth 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]); } } } } // Driver Code let A = [10, 20, 30, 40, 1, 2]; let n = A.length; let X = 91; findFourElements (A, n, X); </script> |
Output
20, 30, 40, 1
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.
Following are the detailed steps.