Optimal game strategy problem 1

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt(),arr[] = new int[n];
for(int i = 0;i<arr.length;i++){
arr[i] = cin.nextInt();
}
findMax(arr,0,1,0,arr.length-1);
}
public static void findMax(int []arr,int p,int count,int l,int h){
if(count >= arr.length){
System.out.println§;
}
if(count % 2 !=0){
if(arr[l]>arr[h]){
findMax(arr,p+arr[l],count+1,l+1,h);
}else{
findMax(arr,p+arr[h],count+1,l,h–);
}
}else{
if(arr[l]>arr[h]){
findMax(arr,p,count+1,l+1,h);
}else{
findMax(arr,p,count+1,l,h–);
}
}
}
}------------what’s the error in my code

@AbhishekAhlawat1102,
I have corrected your code https://ide.codingblocks.com/s/188601

can you explain the logic of pic first and pic last

@AbhishekAhlawat1102,

Calculate all the possible solutions and taking their max at each recursive step as both of them play optically.

Let’s say you and I are playing this game.
I will first choose greater of a[i] or a[j]
then you will choose in such a way that any future move by me gains me minimum profit so bascially you will allow me to choose:

condition1 = a[i] + min( func(i+2 , j ) , func ( i+1 , j-1))
condition2 = a[j] + min( func(i+1 , j -1) , func(i, j-2))

now since you need to play with max value
We will do max( condition1 , condition2)

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.