 Open in App
Not now

# Number of ways whose sum is greater than or equal to K

• Difficulty Level : Hard
• 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 (, ) and (, ) 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], ), ([1, 3], [2, 4]), ([1, 4], [2, 3]), ([2, 3], [1, 4]), ([2, 4], [1, 3]) and (, [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++

 `// C++ code for the above approach:` `#include ``using` `namespace` `std;` `// Function to count ways``int` `cntWays(vector<``int``>& arr, ``int` `K)``{` `    ``int` `ans = 1;` `    ``// Dp will be contain all pairs whose``    ``// sum is from 0 to K-1``    ``vector<``int``> dp(K, 0);` `    ``long` `total = 0;``    ``dp = 1;``    ``int` `lessThanK = K - 1;``    ``for` `(``auto``& n : arr) {` `        ``// Generate pairs for all sum``        ``// from 0 to K-1` `        ``for` `(``int` `i = lessThanK - n; i >= 0; i--) {``            ``dp[i + n] = (dp[i + n] + dp[i]);``        ``}` `        ``// Collecting all power sets``        ``// i.e 2^n``        ``ans = ans * 2;``        ``total += n;``    ``}` `    ``if` `(total < 2 * K)``        ``return` `0;` `    ``// Collect groups whose sum is less``    ``// than K``    ``long` `sumlessthanK = 0;``    ``for` `(``int` `i = 0; i <= lessThanK; ++i) {``        ``sumlessthanK = (sumlessthanK + dp[i]);``    ``}` `    ``// A set less than k will be part of``    ``// either of the two groups, hence we``    ``// multiply by 2 for the same and``    ``// remove it from the power set``    ``ans = (ans - (2 * sumlessthanK));``    ``return` `ans;``}` `// Driver code``int` `main()``{` `    ``vector<``int``> arr{ 1, 2, 3, 4 };``    ``int` `K = 4;` `    ``// Function call``    ``cout << cntWays(arr, K);``    ``return` `0;``}`

## Java

 `import` `java.util.*;` `public` `class` `Gfg {` `  ``// Function to count ways``  ``public` `static` `int` `cntWays(List arr, ``int` `K)``  ``{` `    ``int` `ans = ``1``;` `    ``// Dp will be contain all pairs whose``    ``// sum is from 0 to K-1``    ``List dp``      ``= ``new` `ArrayList<>(Collections.nCopies(K, ``0``));` `    ``long` `total = ``0``;``    ``dp.set(``0``, ``1``);``    ``int` `lessThanK = K - ``1``;``    ``for` `(``int` `n : arr) {` `      ``// Generate pairs for all sum``      ``// from 0 to K-1``      ``for` `(``int` `i = lessThanK - n; i >= ``0``; i--) {``        ``dp.set(i + n, dp.get(i + n) + dp.get(i));``      ``}` `      ``// Collecting all power sets``      ``// i.e 2^n``      ``ans *= ``2``;``      ``total += n;``    ``}` `    ``if` `(total < ``2` `* K)``      ``return` `0``;` `    ``// Collect groups whose sum is less``    ``// than K``    ``long` `sumlessthanK = ``0``;``    ``for` `(``int` `i = ``0``; i <= lessThanK; ++i) {``      ``sumlessthanK += dp.get(i);``    ``}` `    ``// A set less than k will be part of``    ``// either of the two groups, hence we``    ``// multiply by 2 for the same and``    ``// remove it from the power set``    ``ans -= ``2` `* sumlessthanK;``    ``return` `ans;``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String[] args)``  ``{` `    ``List arr = Arrays.asList(``1``, ``2``, ``3``, ``4``);``    ``int` `K = ``4``;` `    ``// Function call``    ``System.out.println(cntWays(arr, K));``  ``}``}` `// This code is contributed by hkdass001.`

## Python3

 `# Python3 code for the above approach``from` `typing ``import` `List` `# Function to count ways``def` `cntWays(arr: ``List``[``int``], K: ``int``) ``-``> ``int``:` `    ``ans ``=` `1` `    ``# Dp will be contain all pairs whose``    ``# sum is from 0 to K-1``    ``dp ``=` `[``0``] ``*` `K` `    ``total ``=` `0``    ``dp[``0``] ``=` `1``    ``lessThanK ``=` `K ``-` `1``    ``for` `n ``in` `arr:` `        ``# Generate pairs for all sum``        ``# from 0 to K-1` `        ``for` `i ``in` `range``(lessThanK ``-` `n, ``-``1``, ``-``1``):``            ``dp[i ``+` `n] ``=` `(dp[i ``+` `n] ``+` `dp[i])` `        ``# Collecting all power sets``        ``# i.e 2^n``        ``ans ``=` `ans ``*` `2``        ``total ``+``=` `n` `    ``if` `total < ``2` `*` `K:``        ``return` `0` `    ``# Collect groups whose sum is less``    ``# than K``    ``sumlessthanK ``=` `0``    ``for` `i ``in` `range``(lessThanK ``+` `1``):``        ``sumlessthanK ``=` `(sumlessthanK ``+` `dp[i])` `    ``# A set less than k will be part of``    ``# either of the two groups, hence we``    ``# multiply by 2 for the same and``    ``# remove it from the power set``    ``ans ``=` `(ans ``-` `(``2` `*` `sumlessthanK))``    ``return` `ans` `# Driver code``if` `__name__ ``=``=` `"__main__"``:` `    ``arr ``=` `[``1``, ``2``, ``3``, ``4``]``    ``K ``=` `4` `    ``# Function call``    ``print``(cntWays(arr, K))` `# This code is contributed by ik_9`

## Javascript

 `// Javascript code for the above approach:` `// Function to count ways``function` `cntWays( arr, K)``{` `    ``let ans = 1;` `    ``// Dp will be contain all pairs whose``    ``// sum is from 0 to K-1``    ``let dp=``new` `Array(K).fill(0);` `    ``let total = 0;``    ``dp = 1;``    ``let lessThanK = K - 1;``    ``for` `(let n of arr) {` `        ``// Generate pairs for all sum``        ``// from 0 to K-1` `        ``for` `(let i = lessThanK - n; i >= 0; i--) {``            ``dp[i + n] = (dp[i + n] + dp[i]);``        ``}` `        ``// Collecting all power sets``        ``// i.e 2^n``        ``ans = ans * 2;``        ``total += n;``    ``}` `    ``if` `(total < 2 * K)``        ``return` `0;` `    ``// Collect groups whose sum is less``    ``// than K``    ``let sumlessthanK = 0;``    ``for` `(let i = 0; i <= lessThanK; ++i) {``        ``sumlessthanK = (sumlessthanK + dp[i]);``    ``}` `    ``// A set less than k will be part of``    ``// either of the two groups, hence we``    ``// multiply by 2 for the same and``    ``// remove it from the power set``    ``ans = (ans - (2 * sumlessthanK));``    ``return` `ans;``}` `// Driver code``let arr=[ 1, 2, 3, 4 ];``let K = 4;` `// Function call``document.write(cntWays(arr, K));`

## C#

 `using` `System;` `public` `class` `GFG{``  ``// Function to count ways``  ``public` `static` `int` `cntWays(``int``[] arr, ``int` `K)``  ``{` `    ``int` `ans = 1;``    ``// Dp will be contain all pairs whose``    ``// sum is from 0 to K-1``    ``int``[] dp = ``new` `int``[K];` `    ``int` `total = 0;``    ``dp = 1;``    ``int` `lessThanK = K - 1;``    ``foreach` `(``int` `n ``in` `arr) {` `      ``// Generate pairs for all sum``      ``// from 0 to K-1``      ``for` `(``int` `i = lessThanK - n; i >= 0; i--) {``        ``dp[i + n] = dp[i + n] + dp[i];``      ``}` `      ``// Collecting all power sets``      ``// i.e 2^n``      ``ans *= 2;``      ``total += n;``    ``}` `    ``if` `(total < 2 * K)``      ``return` `0;` `    ``// Collect groups whose sum is less``    ``// than K``    ``int` `sumlessthanK = 0;``    ``for` `(``int` `i = 0; i <= lessThanK; ++i) {``      ``sumlessthanK += dp[i];``    ``}` `    ``// A set less than k will be part of``    ``// either of the two groups, hence we``    ``// multiply by 2 for the same and``    ``// remove it from the power set``    ``ans -= 2 * sumlessthanK;``    ``return` `ans;``  ``}` `  ``// Driver code``  ``static` `public` `void` `Main (){` `      ``int``[] arr = {1, 2, 3, 4};``      ``int` `K = 4;` `      ``// Function call``      ``Console.Write(cntWays(arr, K));``    ``}``}`

Output

`6`

Time Complexity: O(n*K)
Auxiliary Space: O(sum)

My Personal Notes arrow_drop_up