Optimalstrategy-1

why is it showing error?
required modifications?

@Vibhuti0206
The error is because of your base condition. Your traversal is right to left while the condition is for a left to right traversal . Eventually your value of i becomes negative while your base condition checks for i>n which will never be true. Your code tries to access invalid memory location with negative index.
Change it to i<0.
Even still , your entire approach for this problem is wrong. Kindly read the problem statement again. We cannot sort the array. Also we are allowed to pick elements from both the front and back as the players play optimally. Please try a different approach altogether.