Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication2;
- public class JavaApplication2 {
- public static void main(String[] args) {
- int arr[] = {1,2,3,4,5,6,7,8};
- int sum = 10;
- subSetSum(arr, arr.length, sum);
- }
- static boolean subSetSum(int arr[],int n, int sum){
- boolean mat[][] = new boolean[n+1][sum+1];
- for(int i = 1; i<=sum; i++)
- mat[0][i] = false;
- for(int i = 0; i<=n; i++)
- mat[i][0] = true;
- for(int i = 1; i<=n;i++){
- for(int j = 0;j<=sum;j++){
- mat[i][j] = mat[i-1][j];
- if(j>=arr[i-1]){
- mat[i][j] = mat[i][j] || mat[i-1][j - arr[i-1]];
- }
- }
- }
- for(int x = 0; x<=n;x++){
- for(int y = 0;y<=sum;y++){
- System.out.print(mat[x][y]+" ");
- }
- System.out.println("");
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment