Create a lookup table and fill all entries in it as - 1
int [ ] [ ] dp = RectangularArrays . ReturnRectangularIntArray ( n , n ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = - 1 ; } }