Click here to Skip to main content
15,896,912 members

Comments by Dakush (Top 2 by date)

Dakush 15-Jan-23 5:11am View    
Actually the problem is, say the number of elements in array, n=10. I have to find those elements of array whose sum is zero.
I am thinking of the approach:
1. Find if any two elements make sum 0. If found, then return those elements, otherwise move to next step. (twoSum)
2. Then find if any three elements make sum 0. If found, then return those elements, otherwise move to next step. (threeSum)
3. Then find if any four elements make sum 0. If found, then return those elements, otherwise move to next step. (fourSum)
.
.
n. Then find if all the elements make sum 0. If found, then return those elements, otherwise return no element found. (nSum)
In this case, I have to write separate function for each step. So if n=10, I have to write n fuctions.
I am asking for a general function which will find elements for any value of n.
Dakush 15-Jan-23 5:11am View    
Actually the problem is, say the number of elements in array, n=10. I have to find those elements of array whose sum is zero.
I am thinking of the approach:
1. Find if any two elements make sum 0. If found, then return those elements, otherwise move to next step. (twoSum)
2. Then find if any three elements make sum 0. If found, then return those elements, otherwise move to next step. (threeSum)
3. Then find if any four elements make sum 0. If found, then return those elements, otherwise move to next step. (fourSum)
.
.
n. Then find if all the elements make sum 0. If found, then return those elements, otherwise return no element found. (nSum)
In this case, I have to write separate function for each step. So if n=10, I have to write n fuctions.
I am asking for a general function which will find elements for any value of n.