Chopsticks Problem, I am getting a W.A.

This is the problem statement:-

My solution for it:-
https://www.codechef.com/viewsolution/19763718

Can someone please provide me with a test case which fails in my code? OR any mistake in the code?

1 Like

Hi, in your code in the chop function, with the while loop you have written if( (a[i]-a[i-1]) <= 2). You need to make the 2 to d, i.e. if( (a[i]-a[i-1]) <= d).

This type of mistakes happen as a beginner no issue. But I would like to suggest you to look into this code. Just take a look and see how you can avoid writing the merge-sort function and do it efficiently with the inbuilt library function in C++. Just take a look here :- CodeChef: Practical coding for everyone

I am sure you would not like to spend time coding up a sorting function in C while others will use a inbuilt function. :slight_smile:

Hope this helps. :smiley:

https://www.codechef.com/viewsolution/37399668
please help me I am getting runtime error don’t know why??

Index out of bound in line 16. @i=n-1.

nope not working showing runtime error
??

You just did the opposite.
Index out of bound will occur when i equals (n-1) as you are accessing arr[i+1]. That makes it arr[n] for a vector defined on indices in range [0, n-1].
And hence you are getting a runtime error.

Run your loop from [0, n-1) and you will avoid RE. Or just add a necessary check for index before accessing it.
If (i+1 < n && arr[i+1]…)