4

I was asked this in an interview, and I'm not sure what the answer is or how to approach the problem.

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.

user20598
  • 141

5 Answers5

3

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

2

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

tokam
  • 121
2

This is what i could analyze in a minute or two to make a pattern:

  • Pair Sum as zero: For any pair to be zero, for any number find the negative of that number For example, (10, -10). One exception ofcourse is (0,0).
  • Three Number sum as zero: For 3 numbers, Sum of any two numbers should be equal to the negative of third number. For example, ((1,2), -3) or ((-1,-2),3). So, take sum of two numbers and find the negative of that sum. One possible exception is ofcourse (0,0,0)
  • Four number sum as zero: For 4 numbers, either sum of three numbers should be negative of 4th number or sum of two numbers would be the negative of other two numbers. For example, ((1,2,3),-6) or ((1,4), (-2,-3)). One possible exception would be (0,0,0,0).
2

Incredibly stupid question ....... since it didn't specify UNIQUE numbers the answer is 0!

0 + 0 = 0
0 + 0 + 0 = 0
0 + 0 + 0 + 0 = 0
ennuikiller
  • 1,168
  • 7
  • 8
0

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).

iivel
  • 205