You are given an array nums consisting of positive integers and an integer k.
Partition the array into two ordered groups such that each element is in exactly one group. A partition is called great if the sum of elements of each group is greater than or equal to k.
Return the number of distinct great partitions. Since the answer may be too large, return it modulo 109 + 7.
Two partitions are considered distinct if some element nums[i] is in different groups in the two partitions.
Example 1:
Input: nums = [1,2,3,4], k = 4 Output: 6 Explanation: The great partitions are: ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) and ([4], [1,2,3]).
Example 2:
Input: nums = [3,3,3], k = 4 Output: 0 Explanation: There are no great partitions for this array.
Example 3:
Input: nums = [6,6], k = 2 Output: 2 Explanation: We can either put nums[0] in the first partition or in the second partition. The great partitions will be ([6], [6]) and ([6], [6]).
Constraints:
1 <= nums.length, k <= 10001 <= nums[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a collection of items and you want to separate them into two distinct piles based on a specific numerical rule. The brute force approach works by systematically exploring every conceivable way to distribute the items and checking if each resulting group meets the criteria.
Here's how the algorithm would work step-by-step:
def count_great_partitions(number_list, threshold_sum):
total_combinations = 2 ** len(number_list)
valid_partition_count = 0
modulo_value = 10 ** 9 + 7
# Iterate through all binary representations to simulate every possible distribution of elements into two piles
for combination_mask in range(total_combinations):
first_pile_sum = 0
second_pile_sum = 0
for element_index in range(len(number_list)):
if (combination_mask & (1 << element_index)):
first_pile_sum += number_list[element_index]
else:
second_pile_sum += number_list[element_index]
# Verify if both resulting piles independently satisfy the minimum sum constraint required by the problem
if first_pile_sum >= threshold_sum and second_pile_sum >= threshold_sum:
valid_partition_count = (valid_partition_count + 1) % modulo_value
return valid_partition_countInstead of checking every possible way to split the numbers into two groups, we calculate the total number of all possible group combinations and subtract the bad combinations where one group falls below a required size limit. This clever shortcut lets us find the answer much faster by focusing only on the combinations we want to avoid.
Here's how the algorithm would work step-by-step:
class Solution:
def countGreatPartitions(self, input_numbers: list[int], minimum_limit: int) -> int:
modulo_value = 10**9 + 7
total_sum = sum(input_numbers)
# If the total sum is less than twice the minimum limit, no valid partitions can exist.
if total_sum < 2 * minimum_limit:
return 0
total_combinations = pow(2, len(input_numbers), modulo_value)
dynamic_table = [0] * minimum_limit
dynamic_table[0] = 1
for current_number in input_numbers:
for current_sum in range(minimum_limit - 1, current_number - 1, -1):
dynamic_table[current_sum] = (dynamic_table[current_sum] + dynamic_table[current_sum - current_number]) % modulo_value
invalid_combinations = (2 * sum(dynamic_table)) % modulo_value
# Subtracting invalid combinations from total gives valid ones, accounting for symmetry.
return (total_combinations - invalid_combinations + modulo_value) % modulo_value
| Case | How to Handle |
|---|---|
| Total sum of the array elements is strictly less than 2 times k | Return 0 immediately because it is impossible for both partitions to have a sum of at least k. |
| Minimum array length where length is 2 and each element is less than k | The subset sum logic will correctly result in zero valid partitions and return 0. |
| All elements in the array are individually greater than or equal to k | Every non-empty subset will have a sum at least k, allowing the total valid partitions to be computed using the power of 2 minus invalid subsets. |
| Maximum possible array size and maximum element values leading to large sums | Use a dynamic programming subset sum approach with modulo arithmetic to prevent integer overflow and maintain polynomial time complexity. |
| Duplicate values present in the input array nums | Treat elements as distinct by index in the DP state transition rather than distinct by value to properly count unique structural partitions. |
| An element in nums is much larger than k, exceeding the target threshold instantly | Cap the dynamic programming knapsack table size at k minus 1 to optimize space and time complexity since sums beyond k behave identically for our condition. |
| Extremely large total number of partitions requiring modulo 10 to the 9 plus 7 | Apply modulo arithmetic at every addition and subtraction step to prevent arithmetic overflow and correctly output the answer. |
| No valid great partitions exist even though total sum is at least 2k | The DP subset sum tabulation will find no valid subset sums strictly less than k, correctly resulting in 0 after subtracting invalid combinations. |