Subset Sum Problem SubSet Sum Problem Solution using Knapsack

Categories: Java Java Examples Dynamic programming/Recursion

package dynamic.programing.knapsack;


public class SubsetSumProblemKnapsack {

//Subset Sum Problem-SubSet Sum Problem Solution using Knapsack 

public static void main(String[] args) {

int arr[] = new int[] { 1, 4, 3 };

int sum = 0;

int length = arr.length;

System.out.println("Sub Set Sum Problem Solution using Knapsack :" + subSetKnapsack(arr, sum, length));

System.out.println("Sub Set Sum Problem Solution using Knapsack :" + subSetKnapsackDP(arr, sum, length));

}


private static boolean subSetKnapsack(int[] arr, int sum, int n) {

// 

if (sum == 0)

return true;

if (n == 0)

return false;

if (arr[n - 1] <= sum)

return subSetKnapsack(arr, sum - arr[n - 1], n - 1) || subSetKnapsack(arr, sum, n - 1);

else

return subSetKnapsack(arr, sum, n - 1);

}

private static boolean subSetKnapsackDP(int[] wt, int s, int n) {


boolean dp[][] = new boolean[n + 1][s + 1];

for (int i = 0; i < n + 1; i++) {

for (int j = 0; j < s + 1; j++) {

if (i == 0 )

dp[i][j] = false;

if (j == 0)

dp[i][j] = true;

}

}

for (int i = 1; i < n + 1; i++) {

for (int j = 1; j < s + 1; j++) {


if (wt[i - 1] <= j) {

dp[i][j] =dp[i - 1][j - wt[i - 1]] || dp[i - 1][j];

} else {

dp[i][j] = dp[i - 1][j];

}

}

}

return dp[n][s];

}

}


Top Blogs
All about the java programming language Published at:- When and Why Java is utilized for Application Advancement Published at:- Tips to learn Java Programming Language Published at:- Key Advantages of Java Application Advancement to Assorted Organizations Published at:- Scanner nextLine() ,nextInt() ,nextDouble() method in Java with Examples Published at:- java toUpperCase() and toLowerCase() example Published at:- pushing value at last in java | how to add element at the end of array in java Published at:- fizzbuzz problem java Published at:- Write a program for group words by first character of given string | Java 8 Stream Example Published at:- Write a Java 8 program to calculate the age of a person in years given their birthday Published at:- Write a Java 8 program to calculate the age of a person in years given their birthday Years Months Days Published at:- Write a Java 8 program to print the first 10 odd numbers Published at:- Filter employees by age in Java 8 Lamda steam Published at:- Write a Java 8 program to get the last element of an array string/object Published at:- Filter employees by age set senior if age is greater than 30 in Java 8 Lamda steam Published at:- How to increment salary by 2%, 5%, etc. Using java Published at:- Write a program to find the only duplicate count list in the List Published at:- Write a program to append char in char ex-input- {A, B, C} output->[A_X, B_Y, C_Z] Published at:- Write a program to sum an array without using the sum method Published at:- Write a program to sum an array Published at:- Drop all while condition not meet dropWhile(),dropWhile(Predicate<T> predicate) Published at:- Find the maximum value in a list of integers using Stream & Method Reference Published at:- How to sort a list of strings by length using Lambda expressions Published at:- How to filter and collect a list of strings that start with a specific letter using Java 8 Stream Published at:- Write a Java program To create a map from a array of strings where the key is the string and the value is its length Published at:- How to count the number of occurrences of a given word in a list of strings using Java 8 Published at:- How to remove all duplicates from an array of integers in Java using Java 8 Published at:- How to find next, previous, tomorrow ,yesterday date using Java 8 Published at:- How to iterate and modify values in a Map using Java 8 Published at:- How to print keys & values of a Map using Java 8 Published at:- count of each character in a String using Java 8 Published at:- Write a Program to find the Maximum element in an array Published at:- How to check if list is empty in Java 8 using Optional Published at:- Find duplicate elements with its count using Java 8 Published at:- Find Last duplicate character of given string using Java 8 Published at:- Given a list of integers, separate odd and even numbers Published at:- Singleton Design Pattern and 7 Ways to Implement It 4 Ways to Break Singleton Design Pattern Published at:- java 8 using lambda expression and stream api | Java 8 Programs using Lambda Expression and Stream Published at:- 26 Java 8 Programs using Lambda Expression and Stream Published at:- Java 8 Stream String Null Or Empty Filter Published at:- sum ,min ,max etc example using reduce method using Java 8 Stream API Published at:- Sort highest salary in each department Published at:- Employee Highest salary in each department using Java Published at:- Grouping Employees by City and Age Published at:- Finding the Count of Male and Female Employees Published at:- Printing Names of All Departments from Employee Class using Java Published at:- Printing Employee Details by Age Criteria using Java Published at:- Finding Maximum Age of Employee using Java Published at:- Printing Average Age of Male and Female Employees using Java Published at:- Printing the Number of Employees in Each Department using Java Published at:- Java Class subclass interview coding questions Published at:- Child and Parent overridden methods Throws RuntimeException Published at:- Child and Parent overridden methods Throws checked exception Published at:- Find Customer staring with +91 mobile number Published at:- Concatenate String array into single string Published at:- Sorting the Employees Salary in Each Department in Descending Order Published at:- Sorting the Employees Salary in Each Department in Ascending Order Published at:- Knapsack top down dynamic programming using java Published at:- Subset Sum Problem SubSet Sum Problem Solution using Knapsack Published at:-
R4R.co.in Team
The content on R4R is created by expert teams.