All practice tasks

Coding (Algo)

Count Target Subsets

Meet in the middle · Hard

A batch planner has a small set of indivisible positive workloads and an exact capacity target. It wants to count how many selections fit exactly. Two workloads of the same size are still different selectable items.

Return the number of index subsets whose values sum to target. Each item may be selected at most once.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

A JSON object { "nums": number[], "target": number }.

Fields

  • nums lists positive integer workload sizes.
  • target is a non-negative integer. Subsets may skip any items; unlike a range, selected positions need not be adjacent.

Output

Return value

  • Return the number of selections, not the selections themselves.
  • Equal values at different indexes produce distinct selections.
  • The empty selection counts exactly once when target is 0. Positive values mean no other selection sums to 0.
  • No matching subset returns 0.

Constraints

  • 0 <= nums.length <= 24.
  • 1 <= nums[i] <= 1000000000 and 0 <= target <= 24000000000.
  • The answer is at most 16777216; all relevant arithmetic is exact in JavaScript Number.
  • Do not allocate an array indexed by target. A large target is valid even for a short nums array.

Examples

Example 1

{
  "nums": [
    2,
    2,
    3
  ],
  "target": 5
}

Returns

2

Either copy of 2 may be paired with 3.

Example 2

{
  "nums": [],
  "target": 0
}

Returns

1

The empty selection is one valid subset.

Required complexity

O(2^ceil(n/2)) expected time and space using counted half-sums; O(2^n) enumeration is a baseline to explain but not the requested final approach

What this task practices

Count Target Subsets is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as hard difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.