Number of Great Partitions - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,1], k = 2
Output: 1
💡 Note: Only one great partition: Group A = {1,1} with sum 2, Group B = {2} with sum 2. Both sums ≥ k=2.
Example 2 — Multiple Partitions
$ Input: nums = [3,3,3], k = 4
Output: 3
💡 Note: Three great partitions possible: A={3,3}, B={3} or A={3}, B={3,3} (counted as distinct based on element positions), and A={3} (first), B={3,3} (second,third).
Example 3 — Impossible Case
$ Input: nums = [1,1,1,1], k = 5
Output: 0
💡 Note: Total sum is 4, but we need both groups to have sum ≥ 5. Impossible since 4 < 2×5.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 100
  • 1 ≤ k ≤ 109

Visualization

Tap to expand
Number of Great PartitionsInput: nums = [1,2,1], k = 2121Partition into two groups:Group A{1, 1}sum = 2 ≥ kGroup B{2}sum = 2 ≥ kResult: 1 great partition foundBoth groups satisfy sum ≥ k requirement
Understanding the Visualization
1
Input Array
Given array nums and minimum sum k for each group
2
Partition Elements
Split elements into two groups A and B
3
Count Valid
Count partitions where sum(A) ≥ k and sum(B) ≥ k
Key Takeaway
🎯 Key Insight: Use DP to count valid partitions by tracking possible group sums, avoiding exponential enumeration
Asked in
Google 15 Facebook 12 Amazon 8
12.4K Views
Medium Frequency
~35 min Avg. Time
387 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen