Given an integer array of N elements, the task is to divide this array into K non-empty subsets such that the sum of elements in every subset is same. All elements of this array should be part of exactly one partition.
Input : arr = [2, 1, 4, 5, 6], K = 3 Output : Yes we can divide above array into 3 parts with equal sum as [[2, 4], [1, 5], ] Input : arr = [2, 1, 5, 5, 6], K = 3 Output : No It is not possible to divide above array into 3 parts with equal sum
We can solve this problem recursively, we keep an array for sum of each partition and a boolean array to check whether an element is already taken into some partition or not.
First we need to check some base cases,
If K is 1, then we already have our answer, complete array is only subset with same sum.
If N < K, then it is not possible to divide array into subsets with equal sum, because we can’t divide the array into more than N parts.
If sum of array is not divisible by K, then it is not possible to divide the array. We will proceed only if k divides sum. Our goal reduces to divide array into K parts where sum of each part should be array_sum/K
In below code a recursive method is written which tries to add array element into some subset. If sum of this subset reaches required sum, we iterate for next part recursively, otherwise we backtrack for different set of elements. If number of subsets whose sum reaches the required sum is (K-1), we flag that it is possible to partition array into K parts with equal sum, because remaining elements already have a sum equal to required sum.
Partitions into equal sum is possible.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Number of times an array can be partitioned repetitively into two subarrays with equal sum
- Find all distinct subsets of a given set
- Backtracking to find all subsets
- Root to leaf path sum equal to a given number
- Root to leaf path sum equal to a given number in BST
- Smallest power of 2 which is greater than or equal to sum of array elements
- Shortest root to leaf path sum equal to a given number
- Convert a Binary Tree into its Mirror Tree
- Merge an array of size n into another array of size m+n
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- Split a Numeric String into Fibonacci Sequence
- Print all Jumping Numbers smaller than or equal to a given value
- Combinations where every element appears twice and distance between appearances is equal to the value
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Smallest number with given sum of digits and sum of square of digits
- Find N'th item in a set formed by sum of two arrays
- Find four elements that sum to a given value | Set 2