Memoization approach having error. Please debug!

    public int solve(ArrayList<Integer> A, int B) {
//Using memoization
        int n = A.size();
        int[][] t = new int[n+1][B+1];
        for(int i = 0; i < n+1; i++) {
            for(int j = 0; j < B+1; j++)
                t[i][j] = -1;
        }
        int sol = solveHelper(A,B,n,t);
        return sol;
    }
    
    public int solveHelper(ArrayList<Integer> A, int B, int n, int[][] t) {
        if(n == 0)
            return 0;
        if(B == 0)
            return 1;
        if(t[n][B] != -1)
            return t[n][B];
        
        if(A.get(n - 1) <= B)
            return t[n][B] = Math.max(solveHelper(A,B - A.get(n-1),n-1,t),solveHelper(A,B,n-1,t));
        else
            return t[n][B] = solveHelper(A,B,n-1,t);
    }
}

Here you have applied the wrong base conditions. That’s only a small part of the error.

Write like this:

if(n==0 && b!=0){
return 0;
}

else if(n==0 && b==0){
return 1;
}

else if(b==0){
return 1;
}

If you further want to see the whole code explained properly.

Refer this

Click here to start solving coding interview questions