`{"id":"d972d074-fdaa-44da-9c15-64f5974b99fa","name":"Target Sum Subsets - Dp","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 \r\n up to \"tar\" or not.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number tar","outputFormat":"true or false as required","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;\nvoid input(vector<int> &arr)\n{\n for (int i = 0; i < arr.size(); i++)\n {\n cin >> arr[i];\n }\n}\nvoid targetSumPair(vector<int> &arr, int target)\n{\n //write your code here\n}\nint main()\n{\n int n, target;\n cin >> n;\n vector<int> vec(n, 0);\n input(vec);\n cin >> target;\n targetSumPair(vec, target);\n return 0;\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}"},"python":{"code":"def targetSumPair(arr,target):\n #write your code here\n\n\nn=int(input());\narr=[];\nfor i in range(0,n):\n val=int(input());\n arr.append(val);\ntarget=int(input());\ntargetSumPair(arr,target);"}},"points":10,"difficulty":"medium","sampleInput":"5\r\n4\r\n2\r\n7\r\n1\r\n3\r\n10","sampleOutput":"true","questionVideo":"https://www.youtube.com/embed/CqEsqfidKi0","hints":[],"associated":[{"id":"79f25fd1-2a3b-420d-a05b-9fab8397566e","name":"Another name for Merkel-Hellman cryptosystem is","slug":"another-name-for-merkel-hellman-cryptosystem-is","type":4},{"id":"bb5bb0c6-7cba-4186-809c-4564a3a2569d","name":"DP optimizes the solution by taking advantage of which of the following:","slug":"dp-optimizes-the-solution-by-taking-advantage-of-which-of-the-following","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":"52d62581-1313-45fb-aaf0-1d72a45f6a50","name":"Dynamic Programming And Greedy For Beginners","slug":"dynamic-programming-and-greedy-for-beginners","type":0},{"id":"c6b8409e-63e4-4f6a-a43f-18078ce913a5","name":"Target Sum Subsets - Dp","slug":"target-sum-subsets-dp","type":1}],"next":{"id":"f7674b46-22f3-44e7-8c6b-c11a7c1cfeba","name":"Target Sum Subsets - DP","type":3,"slug":"target-sum-subsets-dp"},"prev":{"id":"47b6a395-4fcf-439e-9dd1-206622dd8b34","name":"Goldmine","type":3,"slug":"goldmine"}}}`

# Target Sum Subsets - Dp

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.

`{"id":"d972d074-fdaa-44da-9c15-64f5974b99fa","name":"Target Sum Subsets - Dp","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 \r\n up to \"tar\" or not.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number tar","outputFormat":"true or false as required","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;\nvoid input(vector<int> &arr)\n{\n for (int i = 0; i < arr.size(); i++)\n {\n cin >> arr[i];\n }\n}\nvoid targetSumPair(vector<int> &arr, int target)\n{\n //write your code here\n}\nint main()\n{\n int n, target;\n cin >> n;\n vector<int> vec(n, 0);\n input(vec);\n cin >> target;\n targetSumPair(vec, target);\n return 0;\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}"},"python":{"code":"def targetSumPair(arr,target):\n #write your code here\n\n\nn=int(input());\narr=[];\nfor i in range(0,n):\n val=int(input());\n arr.append(val);\ntarget=int(input());\ntargetSumPair(arr,target);"}},"points":10,"difficulty":"medium","sampleInput":"5\r\n4\r\n2\r\n7\r\n1\r\n3\r\n10","sampleOutput":"true","questionVideo":"https://www.youtube.com/embed/CqEsqfidKi0","hints":[],"associated":[{"id":"79f25fd1-2a3b-420d-a05b-9fab8397566e","name":"Another name for Merkel-Hellman cryptosystem is","slug":"another-name-for-merkel-hellman-cryptosystem-is","type":4},{"id":"bb5bb0c6-7cba-4186-809c-4564a3a2569d","name":"DP optimizes the solution by taking advantage of which of the following:","slug":"dp-optimizes-the-solution-by-taking-advantage-of-which-of-the-following","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":"52d62581-1313-45fb-aaf0-1d72a45f6a50","name":"Dynamic Programming And Greedy For Beginners","slug":"dynamic-programming-and-greedy-for-beginners","type":0},{"id":"c6b8409e-63e4-4f6a-a43f-18078ce913a5","name":"Target Sum Subsets - Dp","slug":"target-sum-subsets-dp","type":1}],"next":{"id":"f7674b46-22f3-44e7-8c6b-c11a7c1cfeba","name":"Target Sum Subsets - DP","type":3,"slug":"target-sum-subsets-dp"},"prev":{"id":"47b6a395-4fcf-439e-9dd1-206622dd8b34","name":"Goldmine","type":3,"slug":"goldmine"}}}` Editor

# Target Sum Subsets - Dp

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.

## 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

true or false as required

## 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`

Question Video

Discussions

Show Discussion

Related Resources 