`{"id":"eb756042-b717-4dba-8320-5e4a389860f2","name":"Target Sum Subsets","description":"1. You are given a number n, representing the count of elements.\r\n2. You are given n numbers.\r\n3. You are given a number \"tar\".\r\n4. 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.\r\n\r\nNote -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.","inputFormat":"Input Format\r\nA number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number tar","outputFormat":"Comma separated elements of the subset, the contents of which add to \"tar\"\r\n.. all such subsets, each in a single line (the elements of each subset should be comma separated)","constraints":"1 &lt;= n &lt;= 30\r\n0 &lt;= n1, n2, .. n elements &lt;= 20\r\n0 &lt;= tar &lt;= 50","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n // set is the subset\n // sos is sum of subset\n // tar is target\n void printTargetSumSubsets(vector<int> arr, int idx, string set, int sos, int tar){\n //write your code here\n \n }\n\n\nint main(){\n\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n\r\n }\r\n\r\n // set is the subset\r\n // sos is sum of subset\r\n // tar is target\r\n public static void printTargetSumSubsets(int[] arr, int idx, String set, int sos, int tar) {\r\n \r\n }\r\n\r\n}"},"python":{"code":"# asf is the subset\n# sos is sum of subset\n# tar is target\ndef printTargetSumSubsets(arr,idx,asf,sos,tar):\n #write your code here\n\n\n\n\n\n\n\n\ndef main():\n n = int(input());\n arr = []\n for i in range(n):\n arr.append(int(input()));\n \n tar = int(input());\n \nmain()"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n10\r\n20\r\n30\r\n40\r\n50\r\n60","sampleOutput":"10, 20, 30, .\r\n10, 50, .\r\n20, 40, .","questionVideo":"https://www.youtube.com/embed/zNxDJJW40_k","hints":[],"associated":[{"id":"4d176801-4bae-4bce-9fac-23ce4455ae18","name":"(Target Sum Subsets) Subset with target sum can have duplicate elements?","slug":"target-sum-subsets-subset-with-target-sum-can-have-duplicate-elements","type":4},{"id":"ab108df1-d4fc-466a-8c2e-befd87980091","name":"(Target Sum Subsets)How many subsets do an array of n elements have?","slug":"target-sum-subsets-how-many-subsets-do-an-array-of-n-elements-have","type":4},{"id":"e369236e-1528-46d5-b382-1e73d2c36b49","name":"(Target Sum Subsets) What is the time complexity of this approach?","slug":"target-sum-subsets-what-is-the-time-complexity-of-this-approach","type":4},{"id":"ebead098-ed37-4cec-be58-73946b685190","name":"(Target Sum Subsets) What is the space complexity of this approach?","slug":"target-sum-subsets-what-is-the-space-complexity-of-this-approach","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"d341a7c9-1269-409c-b851-0bb512289544","name":"Recursion And Backtracking For Beginners","slug":"recursion-and-backtracking-for-beginners","type":0},{"id":"99046367-231c-4847-8579-3d85ac55501a","name":"Target Sum Subsets","slug":"target-sum-subsets","type":1}],"next":{"id":"268b526d-f047-4f45-b4cd-303ff09103bf","name":"Target Sum Subsets","type":3,"slug":"target-sum-subsets"},"prev":{"id":"6eb3f7aa-b7a2-48a1-a957-9a5e8f0b076b","name":"Flood Fill","type":3,"slug":"flood-fill"}}}`

# 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 -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.

`{"id":"eb756042-b717-4dba-8320-5e4a389860f2","name":"Target Sum Subsets","description":"1. You are given a number n, representing the count of elements.\r\n2. You are given n numbers.\r\n3. You are given a number \"tar\".\r\n4. 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.\r\n\r\nNote -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.","inputFormat":"Input Format\r\nA number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number tar","outputFormat":"Comma separated elements of the subset, the contents of which add to \"tar\"\r\n.. all such subsets, each in a single line (the elements of each subset should be comma separated)","constraints":"1 &lt;= n &lt;= 30\r\n0 &lt;= n1, n2, .. n elements &lt;= 20\r\n0 &lt;= tar &lt;= 50","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n // set is the subset\n // sos is sum of subset\n // tar is target\n void printTargetSumSubsets(vector<int> arr, int idx, string set, int sos, int tar){\n //write your code here\n \n }\n\n\nint main(){\n\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n\r\n }\r\n\r\n // set is the subset\r\n // sos is sum of subset\r\n // tar is target\r\n public static void printTargetSumSubsets(int[] arr, int idx, String set, int sos, int tar) {\r\n \r\n }\r\n\r\n}"},"python":{"code":"# asf is the subset\n# sos is sum of subset\n# tar is target\ndef printTargetSumSubsets(arr,idx,asf,sos,tar):\n #write your code here\n\n\n\n\n\n\n\n\ndef main():\n n = int(input());\n arr = []\n for i in range(n):\n arr.append(int(input()));\n \n tar = int(input());\n \nmain()"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n10\r\n20\r\n30\r\n40\r\n50\r\n60","sampleOutput":"10, 20, 30, .\r\n10, 50, .\r\n20, 40, .","questionVideo":"https://www.youtube.com/embed/zNxDJJW40_k","hints":[],"associated":[{"id":"4d176801-4bae-4bce-9fac-23ce4455ae18","name":"(Target Sum Subsets) Subset with target sum can have duplicate elements?","slug":"target-sum-subsets-subset-with-target-sum-can-have-duplicate-elements","type":4},{"id":"ab108df1-d4fc-466a-8c2e-befd87980091","name":"(Target Sum Subsets)How many subsets do an array of n elements have?","slug":"target-sum-subsets-how-many-subsets-do-an-array-of-n-elements-have","type":4},{"id":"e369236e-1528-46d5-b382-1e73d2c36b49","name":"(Target Sum Subsets) What is the time complexity of this approach?","slug":"target-sum-subsets-what-is-the-time-complexity-of-this-approach","type":4},{"id":"ebead098-ed37-4cec-be58-73946b685190","name":"(Target Sum Subsets) What is the space complexity of this approach?","slug":"target-sum-subsets-what-is-the-space-complexity-of-this-approach","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"d341a7c9-1269-409c-b851-0bb512289544","name":"Recursion And Backtracking For Beginners","slug":"recursion-and-backtracking-for-beginners","type":0},{"id":"99046367-231c-4847-8579-3d85ac55501a","name":"Target Sum Subsets","slug":"target-sum-subsets","type":1}],"next":{"id":"268b526d-f047-4f45-b4cd-303ff09103bf","name":"Target Sum Subsets","type":3,"slug":"target-sum-subsets"},"prev":{"id":"6eb3f7aa-b7a2-48a1-a957-9a5e8f0b076b","name":"Flood Fill","type":3,"slug":"flood-fill"}}}` Editor

# Target Sum Subsets

easy

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 -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.

## Constraints

1 <= n <= 30 0 <= n1, n2, .. n elements <= 20 0 <= tar <= 50

## Format

### Input

Input Format A number n n1 n2 .. n number of elements A number tar

### Output

Comma separated elements of the subset, the contents of which add to "tar" .. all such subsets, each in a single line (the elements of each subset should be comma separated)

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 10 20 30 40 50 60```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}10, 20, 30, . 10, 50, . 20, 40, .```

Question Video

Discussions

Show Discussion

Related Resources 