Optimal game strategy 2 dp

import java.util.*;
public class Main {

public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int[] arr = new int[n];
	for(int i=0;i<n;i++) {
		arr[i] = sc.nextInt();
	}
	
	System.out.println(maxPoints(arr,n));
}

public static int maxPoints(int[] arr,int n) {
	int[][] dp = new int[n][n];
	int gap,x,y,z,i,j;
	
	for(gap=0;gap<n;gap++) {
		for(i=0,j=gap;j<n;i++,j++) {
			x = ((i + 2) <= j) ? dp[i + 2][j] : 0;
			y = ((i + 1) <= (j - 1)) ? dp[i + 1][j - 1] : 0;
			z = (i <= (j - 2)) ? dp[i][j - 2] : 0; 
			  
           dp[i][j] = Math.max(arr[i] + Math.min(x, y),  
                                       arr[j] + Math.min(y, z));
		}
	}
	
	return dp[0][n-1];
}

}

//i got run error in the last case

@Siddharth_sharma1808
your code is correct but its giving run time error because of memory_limit_exceeded.

@Siddharth_sharma1808
test cases are modified now and your code is working fine.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.