From 56edfb4e1ff20d2c8377f9368032c68dc8379ff6 Mon Sep 17 00:00:00 2001 From: hk513 <116027288+hk513@users.noreply.github.com> Date: Mon, 17 Oct 2022 23:37:19 +0530 Subject: [PATCH] target sum subsets 1. You are given a number n, representing the count of elements. 2. You are given n numbers. 3. You are given a number "tar". 4. Complete the body of printTargetSumSubsets function - without changing signature - to calculate and print all subsets of given elements, the contents of which sum to "tar". Use sample input and output to get more idea. note:solved using recusrssion and backtracking --- TargetSumSubsets.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 TargetSumSubsets.java diff --git a/TargetSumSubsets.java b/TargetSumSubsets.java new file mode 100644 index 0000000..29fc889 --- /dev/null +++ b/TargetSumSubsets.java @@ -0,0 +1,38 @@ +import java.io.*; +import java.util.*; + +public class Main { + + public static void main(String[] args) throws Exception { + Scanner scn = new Scanner(System.in); + int n = scn.nextInt(); + int[] arr =new int[n]; + for(int i=0;itar){ + return; + } + if(idx==arr.length){ + if(sos==tar){ + System.out.println(set + "."); + + }return; + } + + printTargetSumSubsets( arr, idx+1, set + arr[idx]+", ", sos+ arr[idx], tar); + printTargetSumSubsets(arr, idx+1, set, sos, tar); + + } + +}