Advanced Coding Patterns for Interviews

0% completed

Previous
Next
Subset Sum Equal to Target (medium)

Problem Statement

Given an array nums containing n integers, return the number of subsets having a sum equal to x.

A subset can be any combination of numbers from the array, including an empty subset. However, you need to consider all possible combinations to determine the exact number of subsets with a sum equal to x.

Examples

Example 1:

  • Input: nums = [1, 2, 3, 4, 5], x = 5
  • Expected Output: 3
  • Explanation: The subsets that sum to 5 are [5], [1, 4], and [2, 3].

Example 2:

  • Input: nums = [2, 4, 6, 10], x = 16
  • Expected Output: 2
  • Explanation: The subsets that sum to 16 are [6, 10] and [2, 4, 10].

Example 3:

  • Input: nums = [7, -3, 2, 5, 1], x = 9
  • Expected Output: 2
  • Explanation: The subsets that sum to 9 are [7, 2] and [-3, 7, 5].

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next