# Split an array containing N elements into K sets of distinct elements

Given two integers N and K and an array arr[] consisting of duplicate elements, the task is to split N elements into K sets of distinct elements.

Examples:

Input: N = 5, K = 2, arr[] = {3, 2, 1, 2, 3}
Output:
( 3 2 1 )
( 2 3 )

Input: N = 5, K = 2, arr[] = {2, 1, 1, 2, 1}
Output: -1
Explanation:
It is not possible to split all the elements into K sets of distinct elements as 1 appears more than K times in the array.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: In order to solve the problem, we are using a map to store the frequency of every element. If the frequency of any element exceeds K, print -1. Maintain another map to store the sets for every respective frequencies. If no element has a frequency greater than K, print the sets for all corresponding frequencies as the required set.

Below is the implementation of the above approach:

 `// C++ Program to split N elements ` `// into exactly K sets consisting ` `// of no distinct elements ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to check if possible to ` `// split N elements into exactly K ` `// sets consisting of no distinct elements ` `void` `splitSets(``int` `a[], ``int` `n, ``int` `k) ` `{ ` `    ``// Store the frequency ` `    ``// of each element ` `    ``map<``int``, ``int``> freq; ` ` `  `    ``// Store the required sets ` `    ``map<``int``, vector<``int``> > ans; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``// If frequency of a ` `        ``// particular element ` `        ``// exceeds K ` `        ``if` `(freq[a[i]] + 1 > k) { ` `            ``// Not possible ` `            ``cout << -1 << endl; ` `            ``return``; ` `        ``} ` ` `  `        ``// Increase the frequency ` `        ``freq[a[i]] += 1; ` `        ``// Store the element for the ` `        ``// respective set ` `        ``ans[freq[a[i]]].push_back(a[i]); ` `    ``} ` ` `  `    ``// Display the sets ` `    ``for` `(``auto` `it : ans) { ` `        ``cout << ``"( "``; ` `        ``for` `(``int` `i : it.second) { ` `            ``cout << i << ``" "``; ` `        ``} ` `        ``cout << ``")\n"``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `arr[] = { 2, 1, 2, 3, 1, ` `                  ``4, 1, 3, 1, 4 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``); ` ` `  `    ``int` `k = 4; ` ` `  `    ``splitSets(arr, n, k); ` ` `  `    ``return` `0; ` `} `

 `// Java program to split N elements  ` `// into exactly K sets consisting  ` `// of no distinct elements  ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `     `  `// Function to check if possible to ` `// split N elements into exactly K ` `// sets consisting of no distinct elements ` `static` `void` `splitSets(``int` `a[], ``int` `n, ``int` `k) ` `{ ` `     `  `    ``// Store the frequency ` `    ``// of each element ` `    ``Map freq = ``new` `HashMap<>(); ` ` `  `    ``// Store the required sets ` `    ``Map> ans = ``new` `HashMap<>(); ` ` `  `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `         `  `        ``// If frequency of a ` `        ``// particular element ` `        ``// exceeds K ` `        ``if``(freq.get(a[i]) != ``null``) ` `        ``{ ` `            ``if` `(freq.get(a[i]) + ``1` `> k)  ` `            ``{ ` `                 `  `                ``// Not possible ` `                ``System.out.println(-``1``); ` `                ``return``; ` `            ``} ` `        ``} ` `         `  `        ``// Increase the frequency ` `        ``freq.put(a[i], freq.getOrDefault(a[i], ``0``) + ``1` `); ` `         `  `        ``// Store the element for the ` `        ``// respective set ` `        ``if``( ans.get(freq.get(a[i])) == ``null``) ` `            ``ans.put(freq.get(a[i]), ` `                    ``new` `ArrayList()); ` `         `  `        ``ans.get(freq.get(a[i])).add(a[i]); ` `    ``} ` `     `  `    ``// Display the sets ` `    ``for``(ArrayList it : ans.values()) ` `    ``{ ` `        ``System.out.print(``"( "``); ` `        ``for``(``int` `i = ``0``; i < it.size() - ``1``; i++) ` `        ``{ ` `            ``System.out.print(it.get(i) + ``" "``); ` `        ``} ` `         `  `        ``System.out.print(it.get(it.size() - ``1``)); ` `        ``System.out.println(``" )"``); ` `    ``} ` `} ` ` `  `// Driver code     ` `public` `static` `void` `main (String[] args) ` `{ ` `    ``int` `arr[] = { ``2``, ``1``, ``2``, ``3``, ``1``, ` `                  ``4``, ``1``, ``3``, ``1``, ``4` `}; ` `                 `  `    ``int` `n = arr.length; ` `    ``int` `k = ``4``; ` `     `  `    ``splitSets(arr, n, k); ` `} ` `} ` ` `  `// This code is contributed by coder001 `

Output:
```( 2 1 3 4 )
( 2 1 3 4 )
( 1 )
( 1 )
```

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.

Check out this Author's contributed articles.

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.

Improved By : coder001

Article Tags :
Practice Tags :