chore(Java): add target-sum-subsets problem (#687)

pull/701/head
Radhika Bansal 2022-02-07 20:28:35 +05:30 committed by GitHub
parent 350072da9e
commit 23595e9524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,49 @@
// Target sum subsets is a program to print all subsets of an array (given by user) such that sum of all elements in subset equal to a target sum given by user
// Algorithm Type: Backtracking
// Time Complexity: O(2^N)
import java.io.*;
import java.util.*;
public class targetSumSubsets {
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
// input size of array
int n = scn.nextInt();
// input elements of array
int[] arr = new int[n];
for(int i=0; i<n; i++){
arr[i] = scn.nextInt();
}
// input target sum
int tar = scn.nextInt();
// calling our function - printTargetSumSubsets
printTargetSumSubsets(arr, 0, "", 0, tar);
scn.close();
}
// set is the subset, sum is the sum so far for that subset
public static void printTargetSumSubsets(int[] arr, int idx, String set, int sum, int tar){
if (idx == arr.length){
if (sum == tar){
System.out.println(set + " ");
}
return;
}
// pick
printTargetSumSubsets(arr, idx + 1, set + arr[idx] + " ", sum + arr[idx], tar);
//not pick
printTargetSumSubsets(arr, idx + 1, set, sum, tar);
}
}