Number of ways whose sum is greater than or equal to K
Last Updated :
24 Mar, 2023
Given an array, arr[], and an integer K, the task is to find the total number of ways in which we can divide the array into two groups such that the sum of each group is greater than equal to K and each element belongs to one of these groups.
Examples:
Input: arr[ ] = [6, 6], K = 2
Output: 2
Explanation: The partitions are ([6], [6]) and ([6], [6]) since both the 6 at index 0 and 1 are treated differently.
Input: arr[ ] = [1, 2, 3, 4], K = 4
Output: 6
Explanation: The 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]).
Approach: The problem can be solved based on the following observation:
Try to solve this problem recursively where we will find out the number of subsets whose sum is less than K and store this in an array to avoid calculating the same thing again and again. Finally, we will subtract this from the total subsets to find the desired answer. This in turn will reduce our time complexity. Yes, you guess it right we are about to apply the dynamic programming concept.
Follow the steps mentioned below to implement the idea:
- If the sum of elements in the array is less than twice K then a way cannot be made.
- we will make dp with all pairs whose sum is less than k
- Generate pairs for all sums from 0 to K – 1
- Collect all the power sets.
- Finally, we will perform an operation where (way whose sum is greater than K = Total partitions – partitions whose (sum < K)).
Below is the Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntWays(vector< int >& arr, int K)
{
int ans = 1;
vector< int > dp(K, 0);
long total = 0;
dp[0] = 1;
int lessThanK = K - 1;
for ( auto & n : arr) {
for ( int i = lessThanK - n; i >= 0; i--) {
dp[i + n] = (dp[i + n] + dp[i]);
}
ans = ans * 2;
total += n;
}
if (total < 2 * K)
return 0;
long sumlessthanK = 0;
for ( int i = 0; i <= lessThanK; ++i) {
sumlessthanK = (sumlessthanK + dp[i]);
}
ans = (ans - (2 * sumlessthanK));
return ans;
}
int main()
{
vector< int > arr{ 1, 2, 3, 4 };
int K = 4;
cout << cntWays(arr, K);
return 0;
}
|
Java
import java.util.*;
public class Gfg {
public static int cntWays(List<Integer> arr, int K)
{
int ans = 1 ;
List<Integer> dp
= new ArrayList<>(Collections.nCopies(K, 0 ));
long total = 0 ;
dp.set( 0 , 1 );
int lessThanK = K - 1 ;
for ( int n : arr) {
for ( int i = lessThanK - n; i >= 0 ; i--) {
dp.set(i + n, dp.get(i + n) + dp.get(i));
}
ans *= 2 ;
total += n;
}
if (total < 2 * K)
return 0 ;
long sumlessthanK = 0 ;
for ( int i = 0 ; i <= lessThanK; ++i) {
sumlessthanK += dp.get(i);
}
ans -= 2 * sumlessthanK;
return ans;
}
public static void main(String[] args)
{
List<Integer> arr = Arrays.asList( 1 , 2 , 3 , 4 );
int K = 4 ;
System.out.println(cntWays(arr, K));
}
}
|
Python3
from typing import List
def cntWays(arr: List [ int ], K: int ) - > int :
ans = 1
dp = [ 0 ] * K
total = 0
dp[ 0 ] = 1
lessThanK = K - 1
for n in arr:
for i in range (lessThanK - n, - 1 , - 1 ):
dp[i + n] = (dp[i + n] + dp[i])
ans = ans * 2
total + = n
if total < 2 * K:
return 0
sumlessthanK = 0
for i in range (lessThanK + 1 ):
sumlessthanK = (sumlessthanK + dp[i])
ans = (ans - ( 2 * sumlessthanK))
return ans
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 ]
K = 4
print (cntWays(arr, K))
|
Javascript
function cntWays( arr, K)
{
let ans = 1;
let dp= new Array(K).fill(0);
let total = 0;
dp[0] = 1;
let lessThanK = K - 1;
for (let n of arr) {
for (let i = lessThanK - n; i >= 0; i--) {
dp[i + n] = (dp[i + n] + dp[i]);
}
ans = ans * 2;
total += n;
}
if (total < 2 * K)
return 0;
let sumlessthanK = 0;
for (let i = 0; i <= lessThanK; ++i) {
sumlessthanK = (sumlessthanK + dp[i]);
}
ans = (ans - (2 * sumlessthanK));
return ans;
}
let arr=[ 1, 2, 3, 4 ];
let K = 4;
document.write(cntWays(arr, K));
|
C#
using System;
public class GFG{
public static int cntWays( int [] arr, int K)
{
int ans = 1;
int [] dp = new int [K];
int total = 0;
dp[0] = 1;
int lessThanK = K - 1;
foreach ( int n in arr) {
for ( int i = lessThanK - n; i >= 0; i--) {
dp[i + n] = dp[i + n] + dp[i];
}
ans *= 2;
total += n;
}
if (total < 2 * K)
return 0;
int sumlessthanK = 0;
for ( int i = 0; i <= lessThanK; ++i) {
sumlessthanK += dp[i];
}
ans -= 2 * sumlessthanK;
return ans;
}
static public void Main (){
int [] arr = {1, 2, 3, 4};
int K = 4;
Console.Write(cntWays(arr, K));
}
}
|
Time Complexity: O(n*K)
Auxiliary Space: O(sum)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...