BackmediumStrings strings medium

Count Distinct Subsets Under Capacity Solution

Problem Statement

Given an array of positive integers `weights` and a positive integer `capacity`, your task is to determine the total count of distinct non-empty subsets of `weights` such that the sum of elements in each subset is less than or equal to `capacity`. Each element from the input `weights` array can be used at most once in a single subset.

Example 1
Input
weights = [1, 2, 3], capacity = 3
Output
4

Explanation: The distinct non-empty subsets whose sum is at most 3 are: [1], [2], [3], and [1, 2].

Constraints

  • 1 <= weights.length <= 15
  • 1 <= weights[i] <= 1000
  • 1 <= capacity <= 15000
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free