Number of Great Partitions - Problem

Imagine you're a team leader dividing your team into two groups for a competitive challenge. You need to ensure both groups are strong enough to handle their tasks effectively.

Given an array nums of positive integers and an integer k, you must partition the array into exactly two ordered groups such that:

  • Each element belongs to exactly one group
  • Both groups have a sum โ‰ฅ k (making them "great" partitions)
  • The order of elements within each group matches their original order in the array

Goal: Count all possible distinct great partitions. Since the answer can be huge, return it modulo 109 + 7.

Example: With nums = [1,1,1,1] and k = 2, you could have group1=[1,1] and group2=[1,1], or group1=[1,1,1] and group2=[1], etc.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,1,1,1], k = 2
โ€บ Output: 6
๐Ÿ’ก Note: Valid partitions: ([1],[1,1,1]), ([1,1],[1,1]), ([1,1,1],[1]), ([1],[1,1,1]), ([1,1],[1,1]), ([1,1,1],[1]). Wait, we need to count distinct partitions based on positions, not values. Actually there are 6 valid ways to split.
example_2.py โ€” Impossible Case
$ Input: nums = [1,2,3], k = 10
โ€บ Output: 0
๐Ÿ’ก Note: Total sum is 6, so it's impossible to have both groups with sum โ‰ฅ 10. No valid partitions exist.
example_3.py โ€” Edge Case
$ Input: nums = [6,6], k = 6
โ€บ Output: 2
๐Ÿ’ก Note: Two valid partitions: ([6],[6]) and ([6],[6]). Since elements are at different positions, both ways count: group1=[nums[0]], group2=[nums[1]] and group1=[nums[1]], group2=[nums[0]].

Visualization

Tap to expand
Team Formation: Split into 2 teams with skill โ‰ฅ kP1P2P3P4Contestants with skills: [1, 1, 1, 1]Team ASkill โ‰ฅ 2Team BSkill โ‰ฅ 2One possible valid partition: A=[1,1], B=[1,1]
Understanding the Visualization
1
Decision Point
At each contestant, decide which team they join
2
Track Progress
Keep track of current team compositions and skill totals
3
Validate & Count
Check if both teams meet requirements and count valid formations
Key Takeaway
๐ŸŽฏ Key Insight: Use dynamic programming to efficiently explore all possible team formations while avoiding redundant calculations through memoization. Track one team's skill total since the other is automatically determined.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— S)

n positions ร— S possible sums (where S is the total sum)

n
2n
โœ“ Linear Growth
Space Complexity
O(n ร— S)

Memoization table size plus recursion stack

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • 1 โ‰ค nums[i] โ‰ค 109
  • 1 โ‰ค k โ‰ค 109
  • Both groups must be non-empty
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
27.4K Views
Medium Frequency
~35 min Avg. Time
890 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