Help in Recursion!!!

Revision en1, by restart., 2015-10-08 17:25:17

Suppose an array A[4]={3,2,10,4} is given. 2 player can pick a number from the front end of the array or last end of the array. if both player wants to make maximum score, what the maximum score first player can make?? in this case first player can make 13 {3,10}. obviously first player starts the picking. I tried this using recursion, but it does not give me correct ans. it gives 17. please help me which states i am missing... I am new to recursion.. TIA..

//here is my code
//initially value=0,left=0,right=n-1=3
int rec(int value,int left,int right)
{
    if(left>right or left>=n or right<0) return 0;
    int p1=value+rec(A[left],left+1,right);
    int p2=value+rec(A[right],left,right-1);
    int mx=max(p1,p2 );
    return mx;
}
Tags recursion, c++

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English restart. 2015-10-08 17:25:17 790 Initial revision (published)