FastPrepPositive Subset Sum
Problem · Array

Positive Subset Sum

Learn this problem
MediumConfluent logoConfluentFULLTIMEONSITE INTERVIEW

Problem statement

You are given an array nums of positive integers and a positive integer target. Return true when some subset of array positions sums exactly to target; otherwise return false.

Each array position may be selected at most once. Equal values at different positions are separate choices.

As an interview follow-up, explain how to reconstruct and return one concrete subset after determining that the target is reachable.

Function

canReachTarget(nums: int[], target: int) → boolean

Examples

Example 1

nums = [12,1,61,5,9,2]target = 24return = true

The positions containing 12, 9, 2, and 1 sum to 24.

Example 2

nums = [2,4,6]target = 5return = false

No subset of the three positive values sums to 5.

Example 3

nums = [5,5,11]target = 10return = true

The two occurrences of 5 occupy different positions and may both be selected.

Constraints

  • 1 <= nums.length <= 200.
  • 1 <= nums[i] <= 10000.
  • 1 <= target <= 20000.

More Confluent problems

drafts saved locally
public boolean canReachTarget(int[] nums, int target) {
    // Write your code here.
}
nums[12,1,61,5,9,2]
target24
expectedtrue
checking account