Google | Max sum of K elements

Hi,

Recently I attended the google interview and here is the question which was asked in the interview.

Givven an array of size N, return max sum of K elements either from start or end or both but not by disturbing the sequence in which the elements occur.

arr 1 5 3 -2 1000 -10
K = 3

output should be: 1 + -10 + 1000 = 991

the combinations to get max sum should be

1 + 5 + 3 = 9
-10 + 1000 + -2 = 988
1 + 5 + -10 = -4
1 + -10 + 1000 = 991

I couldn't solve this question. Could you guys help me in solving it in C/C++?

Comments (13)