We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.util.*;publicclassUnboundedKnapsack{publicstaticintunboundedKnapsack(intk,int[]arr){int[]dp=newint[k+1];for(inti=0;i<=k;i++){for(intnum:arr){if(i-num>=0){dp[i]=Math.max(dp[i],dp[i-num]+num);}}}returndp[k];}publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intt=sc.nextInt();// Number of test casesfor(inttest=0;test<t;test++){intn=sc.nextInt();// Length of arrayintk=sc.nextInt();// Target sumint[]arr=newint[n];for(inti=0;i<n;i++){arr[i]=sc.nextInt();}intresult=unboundedKnapsack(k,arr);System.out.println(result);}sc.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Functions and Fractals - Recursive Trees - Bash!
You are viewing a single comment's thread. Return to all comments →