Array game

problem link:- link text

solution link:-link text

Test case (unlocked) link:-link text


My solution works fine on my pc command prompt. I debugged using test case gives output. But for hackerrank judge gives segmentation fault. Can someone plz tell what is making memory leak.

Your greedy is not quite right consider the case
5 7 10
3 3 3 3 3
4 1 1 1 1 1 1
your code will give ans 3 because it will take 3+3+3 but the actual ans should be 7 (4+1+1+1+1+1+1). So it is not always optimal to select the minimum. I would not ruin your problem by giving its solution try to think a little more. If you can’t solve it, comment

int temp[n];

This does not resize the array temp, instead you are just accessing the mth element of the array which may lead to segmentation fault if m>=n