FastPrepCount Subsets with Sum K
Problems/Count Subsets with Sum K

Count Subsets with Sum K

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interviewer asked: "Count of subsets with sumk." The candidate talked about brute force and dynamic programming.

The report does not say how equal values are counted, whether the empty subset counts, or how large the input is. Here, subsets are chosen by position, the empty subset counts when k is 0, and values are small non-negative numbers.

The judged task matches the report at about 90%.

You are given an integer array nums and an integer k. Return the number of subsets of nums whose elements add up to exactly k.

A subset is chosen by positions, so equal values at different indices are different choices. The empty subset has sum 0, so it is counted when k is 0.

Function

countSubsetsWithSum(nums: int[], k: int) → long

Examples

Example 1

nums = [1,2,3,3]k = 6return = 3

There are two 3s, at indices 2 and 3. The subsets with sum 6 are 1 + 2 + 3 using the first 3, 1 + 2 + 3 using the second 3, and 3 + 3. That makes 3 subsets.

Example 2

nums = [2,0,2]k = 2return = 4

Each 2 alone has sum 2. Adding the 0 to either one keeps the sum at 2, which gives two more subsets. The answer is 4.

Example 3

nums = [5,10]k = 0return = 1

Every element is positive, so only the empty subset has sum 0. The answer is 1.

Constraints

  • 1 <= nums.length <= 30
  • 0 <= nums[i] <= 1000
  • 0 <= k <= 30000

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public long countSubsetsWithSum(int[] nums, int k) {
    // Write your code here.
}
Checking account…