I was asked this in an interview, and I'm not sure what the answer is.
Find a pair of numbers that sum up to zero (or any other number), then find three (and then four) numbers that sum up to zero.
|
I was asked this in an interview, and I'm not sure what the answer is.
|
|||||||||||
|
|
Incredibly stupid question ....... since it didn't specify UNIQUE numbers the answer is 0!
|
|||
|
|
Use the following equality 1+2+3+...+n=n(n+1)/2. 1+(-1) = 0 1+2+(-3) = 0 1+2+3+(-6) = 0 ... 1+2+3+...+n+(-n(n+1)/2) = 0 |
|||||||||
|
|
why don't you just pick a number and it's inverse element in + so for example 1 and -1 ? if you need to find 4 6 8 and so on you can just use 1, -1, 2, -2, 3, -3 and so on. for 3 numbers 3,-2,-1 |
|||
|
|
|
This is what i could analyze in a minute or two to make a pattern:
|
|||
|
|
|
Since you didn't say they had to be integers (or different numbers) Number - Number Number - Number/2 - Number/2 Number - Number/3 - Number/3 - Number/3 Etc. Easy pattern to toss in a loop - though @gpmattoo has a much more elegant solution. (The sum of the first X number minus that sum as the last number). |
|||
|
|