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 ofdistinct great partitions. Since the answer may be too large, return it modulo10^9 + 7.
Two partitions are considered distinct if some element nums[i] is in different groups in the two partitions.
Input: nums =[1,2,3,4], k =4Output: 6Explanation: 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]).
Input: nums =[6,6], k =2Output: 2Explanation: 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]).
Every partition splits nums into two groups. For a partition to be great, both groups must have sum >= k. The total number of partitions is 2^n. We subtract the number of partitions where at least one group has sum < k. By symmetry, this is 2 * (number of subsets with sum < k). But if both groups have sum < k, we’ve double-counted, so we need to add back the number of subsets with sum < k and complement also < k (which is only possible if total < 2k).
#include<vector>#include<numeric>usingnamespace std;
classSolution {
public:int greatPartitions(vector<int>& nums, int k) {
constint MOD =1e9+7;
int n = nums.size();
longlong S = accumulate(nums.begin(), nums.end(), 0LL);
if (S <2LL*k) return0;
vector<longlong> dp(k, 0);
dp[0] =1;
for (int x : nums) {
for (int s = k-1; s >= x; --s) dp[s] = (dp[s] + dp[s-x]) % MOD;
}
longlong bad =0;
for (int s =0; s < k; ++s) bad = (bad + dp[s]) % MOD;
longlong ans = (modpow(2, n, MOD) -2*bad % MOD + MOD) % MOD;
return (int)ans;
}
longlongmodpow(longlong a, longlong b, longlong m) {
longlong r =1;
while (b) {
if (b&1) r = r*a % m;
a = a*a % m; b >>=1;
}
return r;
}
};
classSolution {
publicintgreatPartitions(int[] nums, int k) {
int MOD = (int)1e9+7, n = nums.length, S = 0;
for (int x : nums) S += x;
if (S < 2*k) return 0;
long[] dp =newlong[k];
dp[0]= 1;
for (int x : nums) {
for (int s = k-1; s >= x; --s) dp[s]= (dp[s]+ dp[s-x]) % MOD;
}
long bad = 0;
for (int s = 0; s < k; ++s) bad = (bad + dp[s]) % MOD;
long ans = (modpow(2, n, MOD) - 2*bad % MOD + MOD) % MOD;
return (int)ans;
}
longmodpow(long a, long b, long m) {
long r = 1;
while (b > 0) {
if ((b&1) == 1) r = r*a % m;
a = a*a % m; b >>= 1;
}
return r;
}
}
classSolution {
fungreatPartitions(nums: IntArray, k: Int): Int {
val MOD = 1_000_000_007
val n = nums.size
val S = nums.sum()
if (S < 2*k) return0val dp = LongArray(k)
dp[0] = 1Lfor (x in nums) {
for (s in k-1 downTo x) dp[s] = (dp[s] + dp[s-x]) % MOD
}
var bad = 0Lfor (s in0 until k) bad = (bad + dp[s]) % MOD
val ans = (modpow(2L, n.toLong(), MOD) - 2*bad % MOD + MOD) % MOD
return ans.toInt()
}
funmodpow(a: Long, b: Long, m: Int): Long {
var r = 1L; var aa = a; var bb = b
while (bb > 0) {
if ((bb and 1L) ==1L) r = r*aa % m
aa = aa*aa % m; bb = bb shr 1 }
return r
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defgreatPartitions(self, nums: list[int], k: int) -> int:
MOD =10**9+7 n, S = len(nums), sum(nums)
if S <2*k: return0 dp = [0]*k; dp[0] =1for x in nums:
for s in range(k-1, x-1, -1):
dp[s] = (dp[s] + dp[s-x]) % MOD
bad = sum(dp[:k]) % MOD
ans = (pow(2, n, MOD) -2*bad % MOD + MOD) % MOD
return ans
impl Solution {
pubfngreat_partitions(nums: Vec<i32>, k: i32) -> i32 {
let n = nums.len();
let s: i64= nums.iter().map(|&x| x asi64).sum();
if s <2*k asi64 { return0; }
let m =1_000_000_007;
letmut dp =vec![0i64; k asusize];
dp[0] =1;
for&x in&nums {
for s in (x asusize..k asusize).rev() {
dp[s] = (dp[s] + dp[s-x asusize]) % m;
}
}
let bad: i64= dp.iter().sum::<i64>() % m;
let ans = (modpow(2, n asi64, m) -2*bad % m + m) % m;
ans asi32 }
}
fnmodpow(a: i64, b: i64, m: i64) -> i64 {
letmut r =1;
letmut aa = a; letmut bb = b;
while bb >0 {
if bb &1==1 { r = r*aa % m; }
aa = aa*aa % m; bb >>=1;
}
r
}