Given an array **A[]** consisting of **N** integers, the task is to split the array **A[]** into subsets having equal sum and of length equal to elements in array **B[]**.

**Examples:**

Input:A[] = {17, 13, 21, 20, 50, 29}, B[] = {2, 3, 1}

Output:

21 29

17 13 20

50

Input:A[] = { 1, 2, 3, 4, 5, 6}, B[] = { 2, 2, 2}

Output:

1 6

2 5

3 4

**Approach:** Follow the steps below to solve the problem:

- Since the count of subsets is already given, calculate the sum of each subset.
- Traverse through each element of array B[], find each possible combination of length B[i] and check if the sum of the combination is equal to the desired sum or not.
- Repeat the above steps for every array element B[i] and print the final answer

Below is the implementation of the above approach:

## Python

`# Python Program to implement ` `# the above approach ` `from` `itertools ` `import` `combinations ` ` ` `# Function to split elements of first ` `# array into subsets of size given as ` `# elements in the second array ` `def` `main(A, B): ` ` ` ` ` `# Required sum of subsets ` ` ` `req_sum ` `=` `sum` `(A) ` `/` `/` `len` `(B) ` ` ` ` ` `# Stores the subsets ` ` ` `final ` `=` `[] ` ` ` ` ` `# Iterate the array B[] ` ` ` `for` `i ` `in` `B: ` ` ` ` ` `# Generate all possible ` ` ` `# combination of length B[i] ` ` ` `temp ` `=` `list` `(combinations(A, i)) ` ` ` ` ` `# Iterate over all the combinations ` ` ` `for` `j ` `in` `temp: ` ` ` ` ` `# If the sum is equal to the ` ` ` `# required sum of subsets ` ` ` `if` `(` `sum` `(j) ` `=` `=` `req_sum): ` ` ` ` ` `# Store the subset ` ` ` `temp ` `=` `list` `(j) ` ` ` `final.append(temp) ` ` ` ` ` `for` `k ` `in` `temp: ` ` ` ` ` `# Removing the subset ` ` ` `# from the array ` ` ` `A.remove(k) ` ` ` `break` ` ` ` ` `# Printing the final result ` ` ` `print` `(final) ` ` ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `# Value of array A and B ` ` ` `A ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `] ` ` ` `B ` `=` `[` `2` `, ` `2` `, ` `2` `] ` ` ` ` ` `# Function call ` ` ` `main(A, B) ` |

*chevron_right*

*filter_none*

**Output:**

[[1, 6], [2, 5], [3, 4]]

**Time Complexity:** O(N^{3})

**Auxiliary Space:** O(2^{maxm}), where *maxm *is the maximum element in array B[]

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.

## Recommended Posts:

- Split array into minimum number of subsets having maximum pair sum at most K
- Split numbers from 1 to N into two equal sum subsets
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Split array into K subsets to maximize their sum of maximums and minimums
- Check if an array can be split into subsets of K consecutive elements
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Check if it is possible to split given Array into K odd-sum subsets
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Split array into two subsequences having minimum count of pairs with sum equal to X
- Split Array into min number of subsets with difference between each pair greater than 1
- Count ways to split array into K non-intersecting subsets
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- 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)
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Partition of a set into K subsets with equal sum using BitMask and DP

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.