views:

300

answers:

3

Hi,

I just got this question on a SE position interview, and I'm not quite sure how to answer it, other than brute force:

Given a natural number N, find two numbers, A and P, such that:

N = A + (A+1) + (A+2) + ... + (A+P-1)

P should be the maximum possible.

Ex: For N=14, A = 2 and P = 4

N = 2 + (2+1) + (2+2) + (4+2-1) N = 2 + 3 + 4 + 5

Any ideas?

+9  A: 

If N is even/odd, we need an even/odd number of odd numbers in the sum. This already halfes the number of possible solutions. E.g. for N=14, there is no point in checking any combinations where P is odd.

Rewriting the formula given, we get:

N = A + (A+1) + (A+2) + ... + (A+P-1)
    = P*A + 1 + 2 + ... + (P-1)
    = P*A + (P-1)P/2 *
    = P*(A + (P-1)/2)
    = P/2*(2*A + P-1)

The last line means that N must be divisible by P/2, this also rules out a number of possibilities. E.g. 14 only has these divisors: 1, 2, 7, 14. So possible values for P would be 2, 4, 14 and 28. 14 and 28 are ruled our for obvious reasons (in fact, any P above N/2 can be ignored).

This should be a lot faster than the brute-force approach.

(* The sum of the first n natural numbers is n(n+1)/2)

Uli Schlachter
I'm not sure if I get your idea: Could you explain it for `15`?As far as I understand you, 15 has divisors 1,3,5,15 (clear). Thus, we have possible values for P: 2,6,10,30. But the value for P should be 5 since 15=1+2+3+4+5, shouldn't it?
phimuemue
1+2+3+4+5 = 0+(0+1)+(0+2)+(0+3)+(0+4)+(0+5) is with A=0 and P=6, no?
Uli Schlachter
I think you mean N*2 must be divisible by P. For N=15 that gives P in +-{1,2,3,5,6,10,15,30}, although we can ignore the negatives because P=1 always works.
Strilanc
@Strilanc: Sorry, of course you are right. The negative values for P can be ignored, I guess.@phimuemue: If A has to be positive, then A=0, P=6 is not a valid solution.
Uli Schlachter
(in fact, any P above N/2 can be ignored) - I guess P < sqrt(2N) is a good check as well
DK
+1  A: 

A + (A+1) + (A+2) + ... + (A+P-1) simplifies to P*A + (P*(P-1)/2) resp P*(A+(P-1)/2).

Thus, you could just enumerate all divisors of N, and then test each divisor P to the following:

Is A = (N-(P*(P-1)/2))/P (solved the first simplification for A) an integral number? (I assume it should be an integral number, otherwise it would be trivial.) If so, return it as a solution.

phimuemue
"just enumerate all divisors of N" is not so easy...its equivalent to doing a prime factorization
frankc
@user275455 Finding factors of N is still faster than iterating from 1 to N.
Strilanc
(assuming constant-time division)
Strilanc
+3  A: 

With interview questions, it is often wise to think about what is probably the purpose of the question. If I would be asking you this question, it is not because I think you know the solution, but I want to see you finding the solution. Reformulating the problem, making implications, devising what is known, ... this is what I would like to see.

  • If you just sit and tell me "I do not know how to solve it", you immediately fail the interview.

  • If you say: I know how to solve it by brute force, and I am aware it will be probably slow, I will give you some hints or help you to get you started. If that does not help, you most likely fail (unless you show some extraordinary skills to compensate for the fact you are probably lacking something in the field of general problem analysis, e.g. you will show how to implement a solution paralelized for many cores or implemented on GPU).

  • If you bring me a ready solution, but you are unable to derive it, I will give you another similar problem, because I am not interested about solution, I am interested in your thinking.

Suma
I agree, however, this was a written exam with no interaction with the interviewer, and the paper only had the description above! I did try to write my thoughts and if later I thought why that was not going to work, I crossed it with a line and wrote why. I guess that's the equivalent
Dan