Given an **array arr** of natural numbers up to **n**, find the maximum size for which array **arr** can be divided into two equal-sized arrays, such that the first array contains all same elements while the second array contains all distinct elements.

**Examples:**

Input :n = 8, arr[] ={7, 3, 7, 1, 7, 7}

Output :

Maximum size is : 3

arr1[] ={7, 7, 7}

arr2[] ={1, 3, 7}

Explanation :

It is possible to construct two arrays of size 3.

The first array is [7, 7, 7] and the second array is [1, 3, 7].

Input :n = 7, arr[] ={1, 2, 1, 5, 1, 6, 7, 2}

Output :

Maximum size is : 3

arr1[] ={1, 1, 1}

arr2[] ={2, 5, 6}

**Approach: **

To solve the problem mentioned above the main idea is to use hashing to find the frequency of every element present in the array.

- Find the element with maximum frequency present in the array arr[] using hash vector v.
- Find the total unqiue elements present in array arr[].
- There are two cases for the element with maximum frequency: the maximum frequency element will go to the first array then the sizes of array are at most
**diff1 – 1**and**max1**correspondingly. Otherwise, at least one element of maximum frequency goes to the second array and the sizes are at most**diff1**and**max1**?**1**correspondingly. Then find the max-size to which array can be splitted as**max(min(diff1 ? 1, max1), min(diff1, max1 ? 1))**. - Find the first array of similar elements using max_size and element with maximum frequency max1.
- Find the second array of unique elements using max_size and hash vector v.

Below is the implementation of the above approach:

`// C++ program to find the max-size to which ` `// an array can be divided into 2 equal parts ` `// such that one part contains unique elements ` `// while another contains similar elements ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the max-size to which an array ` `// can be divided into 2 equal parts ` `void` `Solve(` `int` `arr[], ` `int` `size, ` `int` `n) ` `{ ` ` ` ` ` `vector<` `int` `> v(n + 1); ` ` ` ` ` `// Vector to find the frequency of ` ` ` `// each element of array ` ` ` `for` `(` `int` `i = 0; i < size; i++) ` ` ` `v[arr[i]]++; ` ` ` ` ` `// Find the maximum frequency ` ` ` `// element present in array arr[] ` ` ` `int` `max1 = (max_element(v.begin(), v.end()) ` ` ` `- v.begin()); ` ` ` ` ` `// Find total unique elements ` ` ` `// present in array arr[] ` ` ` `int` `diff1 = n + 1 - count(v.begin(), v.end(), 0); ` ` ` ` ` `// Find the Max-Size to which ` ` ` `// an array arr[] can be splitted ` ` ` `int` `max_size = max(min(v[max1] - 1, diff1), ` ` ` `min(v[max1], diff1 - 1)); ` ` ` ` ` `cout << ` `"Maximum size is :"` `<< max_size << ` `"\n"` `; ` ` ` ` ` `// Find the first array ` ` ` `// containing same elements ` ` ` `cout << ` `"The First Array Is : \n"` `; ` ` ` `for` `(` `int` `i = 0; i < max_size; i++) { ` ` ` `cout << max1 << ` `" "` `; ` ` ` `v[max1] -= 1; ` ` ` `} ` ` ` ` ` `cout << ` `"\n"` `; ` ` ` ` ` `// Find the second array ` ` ` `// containing unique elements ` ` ` `cout << ` `"The Second Array Is : \n"` `; ` ` ` `for` `(` `int` `i = 0; i < (n + 1); i++) { ` ` ` `if` `(v[i] > 0) { ` ` ` `cout << i << ` `" "` `; ` ` ` `max_size--; ` ` ` `} ` ` ` `if` `(max_size < 1) ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `cout << ` `"\n"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// initialise n ` ` ` `int` `n = 7; ` ` ` ` ` `// array declaration ` ` ` `int` `arr[] = { 1, 2, 1, 5, 1, 6, 7, 2 }; ` ` ` ` ` `// size of array ` ` ` `int` `size = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `Solve(arr, size, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Maximum size is :3 The First Array Is : 1 1 1 The Second Array Is : 2 5 6

**Time Commplexity :**O(N)

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:

- Divide array into two sub-arrays such that their averages are equal
- Number of ways to divide an array into K equal sum sub-arrays
- Divide the array into minimum number of sub-arrays having unique elements
- Find all numbers that divide maximum array elements
- Find maximum sum array of length less than or equal to m
- Count arrays of length K whose product of elements is same as that of given array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Maximum length of the sub-array whose first and last elements are same
- Maximum length of subarray such that all elements are equal in the subarray
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Number of non-decreasing sub-arrays of length less than or equal to K
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Divide binary array into three equal parts with same value
- Number of non-decreasing sub-arrays of length greater than or equal to K
- Equally divide into two sets such that one set has maximum distinct elements
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Count the number of ways to divide an array into three contiguous parts having equal sum
- Count array elements that divide the sum of all other elements
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Count sub-arrays which have elements less than or equal to X

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.