470. Count Special Subsets
Count Special Subsets

Given a list of integers a, choose any subset S of its indices. Let sum(S) be the sum of the selected values. The subset is special if sum(S) ≥ a[j] for every unselected index j.

Rules

  • Indices are zero-based. Different index subsets count separately, even when their values are equal.
  • The empty subset has sum 0 and product 1. It is special only if every value in a is at most 0.
  • Selecting all indices is always special because no unselected indices remain.
  • Check the condition using ordinary integer sums. Return every answer modulo 1,000,000,007, in the range 0 to 1,000,000,006.

Method Signatures

Each method receives the same input parameter a, the list of integers.

Count Special Subsets

int countSpecialSubsets(List<Integer> a)
  • Return the number of special subsets.

Follow-Up 1: Sum of Sums

int sumOfSpecialSubsetSums(List<Integer> a)
  • Return the total obtained by adding the sum of each special subset.

Follow-Up 2: Sum of Products

int sumOfSpecialSubsetProducts(List<Integer> a)
  • Return the total obtained by adding the product of the selected values in each special subset.

Constraints

  • 1 ≤ a.size() ≤ 100
  • -1,000 ≤ a[i] ≤ 1,000

Examples

Example 1

countSpecialSubsets(a = List.of(2, 3, 4))
Output: 5

sumOfSpecialSubsetSums(a = List.of(2, 3, 4))
Output: 31

sumOfSpecialSubsetProducts(a = List.of(2, 3, 4))
Output: 54

The special subsets contain values [4], [2, 3], [2, 4], [3, 4], and [2, 3, 4]. Their sums total 31, and their products total 54.

Example 2

countSpecialSubsets(a = List.of(-3, 2))
Output: 2

sumOfSpecialSubsetSums(a = List.of(-3, 2))
Output: 1

sumOfSpecialSubsetProducts(a = List.of(-3, 2))
Output: 1000000003

The special subsets contain [2] and [-3, 2]. Their sums total 1. Their products total -4, whose nonnegative remainder is 1000000003.

Example 3

countSpecialSubsets(a = List.of(0, 0))
Output: 4

sumOfSpecialSubsetSums(a = List.of(0, 0))
Output: 0

sumOfSpecialSubsetProducts(a = List.of(0, 0))
Output: 1

All four index subsets are special. Every sum is 0. Nonempty subsets have product 0, while the empty subset contributes product 1.



Please use Laptop/Desktop or any other large screen to add/edit code.