Confusion in 0-1 Knapsack problem

PLEASE CHECK WHERE I AM DOING MISTAKE.

import java.util.*;
public class KnapSack01
{
public static int knapsack(int S,int size[],int value[],int n)
{
int i,s;
int DP[][]=new int[n+1][S+1];
for(i=0;i<=n;i++)
{
for(s=0;s<=S;s++)
{
if(i==0||s==0)
DP[i][s]=0;
else if(size[i-1]<=s)
DP[i][s]=Math.max(value[i-1]+DP[i-1][s-size[i-1]], DP[i-1][s]);
else
DP[i][s]=DP[i-1][s];
}
}
return DP[n][S];
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int S=sc.nextInt();
int[] sizes=new int[N];
int[] values=new int[N];
System.out.println(knapsack(S, sizes, values, N));
}
}

@vinay86048,
https://ide.codingblocks.com/s/238171 corrected code.

Your knapsack logic is correct buddy. You are not taking input in your arrays. That’s why the error.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.