I got my mistake...i have to close this question
Problem Name-Subset Sum Problem
Problem Link-https://www.codechef.com/problems/TF01
Solution Link-https://www.codechef.com/viewsolution/13633586
I am new to dp,please please help me to improve my code.
My Code---->:
class subsetsumdp
{
static boolean isexistsubset(int set[],int sum,int n)
{
Arrays.sort(set);
boolean dp[][]=new boolean[n][sum+1];
for(int i=0;i<n;i++)
dp[i][0]=true;
dp[0][set[0]]=true;//set the first row of dp table
for(int i=1;i<n;i++)
{
for(int j=1;j<=sum;j++)
{
if(set[i]<j)
dp[i][j]=dp[i-1][j]||dp[i-1][j-set[i]];
else
dp[i][j]=dp[i-1][j];
}
}
return dp[n-1][sum];
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t!=0)
{
int n=sc.nextInt();
int sum=sc.nextInt();
int set[]=new int[n];
for(int i=0;i<n;i++)
set[i]=sc.nextInt();
boolean ans=isexistsubset(set, sum,n);
//if subset present or not
if(ans==true)
System.out.println("1");
else
System.out.println("0");
t--;}
}
}
asked
19 May '17, 11:41
2★vivek96
518●2●12
accept rate:
7%