Subset Sum to Target
Updated
Problem statement
You are given an array nums of signed integers and a signed integer target. Determine whether some subset of array indices has values summing exactly to target. Each index may be selected at most once, and the empty subset is allowed.
Return 1 if such a subset exists; otherwise return 0.
Function
hasSubsetSum(nums: int[], target: int) → intExamples
Example 1
nums = [3,34,4,12,5,2]target = 9return = 1Selecting 4 and 5 reaches the target 9.
Example 2
nums = [2,-4,7]target = 1return = 0The possible non-empty sums are 2, -4, 7, -2, 9, 3, and 5, so no subset reaches 1.
Constraints
0 <= nums.length <= 30.-10^9 <= nums[i], target <= 10^9.- Use 64-bit arithmetic for intermediate subset sums.
More Dream11 Onsite Interview questions
Similar questions at other companies
- Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
- Distinct Subset SumsGoogle · ONSITE INTERVIEWGoogle interview questions
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Coin ChangeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Maximum SubarrayUpstart · ONSITE INTERVIEWUpstart interview questions
- Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
- Maximum Product SubarrayHartford Financial Services · ONSITE INTERVIEWHartford Financial Services interview questions
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions