• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
July 15, 2024 |10.5K Views

Find four elements that sum to a given value | Set 1 (n^3 solution)

Description
Discussion

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 
 

Find four elements that sum to a given value _ Set 1 (n^3 solution): https://www.geeksforgeeks.org/find-four-numbers-with-sum-equal-to-given-sum/