Subset Sum Equal To K
Dynamic Programming
medium
Score: 30
Given an array of integers nums
and an integer k
, return 1
if there exists a subset in nums
with a sum equal to k
. otherwise, return 0
Input Format
First Parameter: Length of array n
Second Parameter: Array of int nums
Third Parameter: Integer k
Output Format
Return the integer.
Example 1
Input: nums = [1,2,3,4], k = 4
Output: 1
Explanation: There exist 2 subsets with sum = 4. These are [1,3] and [4]. Hence, return 1.
Example 2
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no subset with a sum equal to 1.
Constraints
- 1 <= nums.length <= 103
- 0 <= nums[i] <= 109
- 0 <= k <= 103
- Expected Time Complexity - O(n*k)
- Expected Space Complexity - O(n*k)