`{"id":"05650d94-4fc7-4d78-a196-a7b261a62f42","name":"Print All Paths With Target Sum Subset","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. You are required to calculate and print true or false, if there is a subset the elements of which add up to \"tar\" or not.\r\n5. Also, you have to print the indices of elements that should be selected to achieve the given target.\r\n6. You have to print all such configurations.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number tar","outputFormat":"Check the sample output and question video.","constraints":"1 &lt;= n &lt;= 30\r\n0 &lt;= n1, n2, .. n elements &lt;= 20\r\n0 &lt;= tar &lt;= 50","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class Pair{\r\n int i;\r\n int j;\r\n String psf;\r\n\r\n public Pair(int i, int j, String psf){\r\n this.i = i;\r\n this.j = j;\r\n this.psf = psf;\r\n }\r\n }\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n int[] arr = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(br.readLine());\r\n }\r\n\r\n int tar = Integer.parseInt(br.readLine());\r\n\r\n //write your code here\r\n\r\n }\r\n}\r\n\r\n\r\n \r\n\r\n\r\n "},"node":{"code":""},"ruby":{"code":""},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"5\r\n4\r\n2\r\n7\r\n1\r\n3\r\n10","sampleOutput":"true\r\n2 4 \r\n1 2 3 \r\n0 1 3 4 \r\n","questionVideo":"https://www.youtube.com/embed/qtqMTgmDpQg?end=91","hints":[],"associated":[],"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":"5539a6e8-c8bf-4f04-805c-e43e9d20e72a","name":"Dynamic Programming For Intermediate","slug":"dynamic-programming-for-intermediate-408","type":0},{"id":"97f76df1-b539-4665-9a42-78de87589bf8","name":"Print All Paths With Target Sum Subset","slug":"print-all-paths-with-target-sum-subset","type":1}],"next":{"id":"834ad99b-1724-4e8e-a373-d22c9a5637b0","name":"print all paths with target sum subset MCQ","type":0,"slug":"print-all-paths-with-target-sum-subset-mcq"},"prev":{"id":"ab2290d9-31a6-42cd-8d37-911312b9ec43","name":"Count of Distinct Palindromic Subsequence","type":3,"slug":"count-of-distinct-palindromic-subsequence"}}}`

# Print All Paths With Target Sum Subset

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. You are required to calculate and print true or false, if there is a subset the elements of which add up to "tar" or not. 5. Also, you have to print the indices of elements that should be selected to achieve the given target. 6. You have to print all such configurations.

`{"id":"05650d94-4fc7-4d78-a196-a7b261a62f42","name":"Print All Paths With Target Sum Subset","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. You are required to calculate and print true or false, if there is a subset the elements of which add up to \"tar\" or not.\r\n5. Also, you have to print the indices of elements that should be selected to achieve the given target.\r\n6. You have to print all such configurations.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number tar","outputFormat":"Check the sample output and question video.","constraints":"1 &lt;= n &lt;= 30\r\n0 &lt;= n1, n2, .. n elements &lt;= 20\r\n0 &lt;= tar &lt;= 50","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class Pair{\r\n int i;\r\n int j;\r\n String psf;\r\n\r\n public Pair(int i, int j, String psf){\r\n this.i = i;\r\n this.j = j;\r\n this.psf = psf;\r\n }\r\n }\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n int[] arr = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(br.readLine());\r\n }\r\n\r\n int tar = Integer.parseInt(br.readLine());\r\n\r\n //write your code here\r\n\r\n }\r\n}\r\n\r\n\r\n \r\n\r\n\r\n "},"node":{"code":""},"ruby":{"code":""},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"5\r\n4\r\n2\r\n7\r\n1\r\n3\r\n10","sampleOutput":"true\r\n2 4 \r\n1 2 3 \r\n0 1 3 4 \r\n","questionVideo":"https://www.youtube.com/embed/qtqMTgmDpQg?end=91","hints":[],"associated":[],"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":"5539a6e8-c8bf-4f04-805c-e43e9d20e72a","name":"Dynamic Programming For Intermediate","slug":"dynamic-programming-for-intermediate-408","type":0},{"id":"97f76df1-b539-4665-9a42-78de87589bf8","name":"Print All Paths With Target Sum Subset","slug":"print-all-paths-with-target-sum-subset","type":1}],"next":{"id":"834ad99b-1724-4e8e-a373-d22c9a5637b0","name":"print all paths with target sum subset MCQ","type":0,"slug":"print-all-paths-with-target-sum-subset-mcq"},"prev":{"id":"ab2290d9-31a6-42cd-8d37-911312b9ec43","name":"Count of Distinct Palindromic Subsequence","type":3,"slug":"count-of-distinct-palindromic-subsequence"}}}`

Editor

# Print All Paths With Target Sum Subset

medium

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. You are required to calculate and print true or false, if there is a subset the elements of which add up to "tar" or not. 5. Also, you have to print the indices of elements that should be selected to achieve the given target. 6. You have to print all such configurations.

## Constraints

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

## Format

### Input

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

### Output

Check the sample output and question video.

## 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 4 2 7 1 3 10```

### 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;}true 2 4 1 2 3 0 1 3 4 ```

Question Video

Discussions

Show Discussion

Related Resources