Advanced Coding Patterns for Interviews

0% completed

Previous
Next
Subsets having Sum between A and B (hard)

Problem Statement

You are given an integer array nums containing n integers, integer A and integer B.

Return the number of subsets of the nums have a sum that falls between the values A and B (inclusive).

Examples

Example 1:

  • Input: nums = [1, -1, 2, 3], A = 1, B = 4
  • Expected Output: 10
  • Justification: The subsets that satisfy the condition are: [1], [2], [3], [1, 2], [1, 3], [-1, 2], [-1, 3], [1, -1, 2], [-1, 2, 3] and [1, -1, 3]. Each of these subsets has a sum between 1 and 4.

Example 2:

  • Input: nums = = [3, 5, -7], A = -4, B = 3
  • Expected Output: 5
  • Justification: The subsets that satisfy the condition are: [], [3], [5, -7], [3, -7], and [3, 5, -7]. Each of these subsets has a sum between -4 and 3.

Example 3:

  • Input: nums = [4, 1, -2, 7, -3], A = 0, B = 6
  • Expected Output: 16
  • Justification: There are a total 16 subsets which have a sum in the range 0 to 6.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

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