<gouthampradhan>dynamic_programming_StoneGame_1.java
private int dp(int[][] A, int[] piles, int i, int j, int sum, int p, int sumA, int sumB) {
    if (A[p][sum] != -1) return A[p][sum];
    else {
      if (p == 0) {
        if (i <= j) {
          int result = dp(A, piles, i + 1, j, sum - piles[i], (p + 1) % 2, sumA + piles[i], sumB);
          if (result == 0) {
            A[p][sum] = 1;
            return 1;
          } else {
            result = dp(A, piles, i, j - 1, sum - piles[j], (p + 1) % 2, sumA + piles[j], sumB);
            A[p][sum] = result;
            return result;
          }
        } else {
          if (sumA > sumB) return 1;
          else return 0;
        }
      } else {
        if (i <= j) {
          int result = dp(A, piles, i + 1, j, sum - piles[i], (p + 1) % 2, sumA, sumB + piles[i]);
          if (result == 0) {
            A[p][sum] = 1;
            return 1;
          } else {
            result = dp(A, piles, i, j - 1, sum - piles[j], (p + 1) % 2, sumA, sumB + piles[j]);
            A[p][sum] = result;
            return result;
          }
        } else {
          if (sumB > sumA) return 1;
          else return 0;
        }
      }
    }
  }
private int sample_funcname ( int [ ] [ ] A , int [ ] piles , int i , int j , int sum , int p , int sumA , int sumB ) {
  if ( A [ p ] [ sum ] != - 1 ) return A [ p ] [ sum ] ;
  else {
    if ( p == 0 ) {
      if ( i <= j ) {
        int result = dp ( A , piles , i + 1 , j , sum - piles [ i ] , ( p + 1 ) % 2 , sumA + piles [ i ] , sumB ) ;
        if ( result == 0 ) {
          A [ p ] [ sum ] = 1 ;
          return 1 ;
        }
        else {
          result = dp ( A , piles , i , j - 1 , sum - piles [ j ] , ( p + 1 ) % 2 , sumA + piles [ j ] , sumB ) ;
          A [ p ] [ sum ] = result ;
          return result ;
        }
      }
      else {
        if ( sumA > sumB ) return 1 ;
        else return 0 ;
      }
    }
    else {
      if ( i <= j ) {
        int result = dp ( A , piles , i + 1 , j , sum - piles [ i ] , ( p + 1 ) % 2 , sumA , sumB + piles [ i ] ) ;
        if ( result == 0 ) {
          A [ p ] [ sum ] = 1 ;
          return 1 ;
        }
        else {
          result = dp ( A , piles , i , j - 1 , sum - piles [ j ] , ( p + 1 ) % 2 , sumA , sumB + piles [ j ] ) ;
          A [ p ] [ sum ] = result ;
          return result ;
        }
      }
      else {
        if ( sumB > sumA ) return 1 ;
        else return 0 ;
      }
    }
  }
}
