This is my code . Only 1 test case is failing , due to run time error . Can you tell me where did I go wrong ?
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in) ;
int n = scan.nextInt() ;
int target = scan.nextInt() ;
int[] arr = new int[n] ;
for(int i = 0 ; i < n ; i ++){
arr[i] = scan.nextInt() ;
}
int ans = TSBU(arr,target) ;
if(ans == 1)
System.out.print("Yes") ;
else
System.out.print("No") ;
}
// public static boolean TS(int[] arr , int vi , int target, int[][] strg){
// if(target == 0){
// return true ;
// }
// if(target < 0 || vi == arr.length){
// return false ;
// }
// if(strg[vi][target] != -1){
// if(strg[vi][target] == 1)
// return true ;
// else
// return false ;
// }
// boolean i = TS(arr,vi+1,target-arr[vi],strg) ;
// boolean e = false ;
// if(!i){
// e = TS(arr,vi+1,target,strg) ;
// }
// boolean ans = i || e ;
// if(ans){
// strg[vi][target] = 1 ;
// }
// else{
// strg[vi][target] = 0 ;
// }
// return i || e ;
// }
public static int TSBU(int[] arr , int target){
int n = arr.length ;
int[][] strg = new int[n+1][target+1] ;
for(int i = n ; i >= 0 ; i --){
for(int j = target ; j >= 0 ; j --){
if (j == 0){
strg[i][j] = 1 ;
}
else if(i == n){
strg[i][j] = 0 ;
}
else{
int in = 0 ;
if(j >= arr[i]){
in = strg[i+1][j-arr[i]] ; // including the element
}
int e = strg[i+1][j] ; // excluding the element
if(in == 0 && e == 0){
strg[i][j] = 0 ;
}else{
strg[i][j] = 1 ;
}
}
}
}
//display(strg) ;
return strg[0][target] ;
}
public static void display(int[][] strg){
for(int i = 0 ; i < strg.length ; i ++){
for(int j = 0 ; j < strg[0].length ; j ++){
System.out.print(strg[i][j]+" ") ;
}
System.out.println() ;
}
}
}