You are not logged in. Please login at www.codechef.com to post your questions!

×

[closed] what is wrong in my code?

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

vivek96's gravatar image

2★vivek96
518212
accept rate: 7%

closed 19 May '17, 13:28

The question has been closed for the following reason "i got my mistake" by vivek96 19 May '17, 13:28

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,284
×1,690
×939
×180
×32

question asked: 19 May '17, 11:41

question was seen: 255 times

last updated: 19 May '17, 13:28