Given an array **arr[]**, the task is to determine the maximum possible size of two arrays of the same size that can be created using the given array elements such that in one array all the elements are **distinct** and in the other, all elements are the** same**. **Note:** It is not mandatory to use all the elements of the given array to build the two arrays.**Examples:**

Input:a[] = { 4, 2, 4, 1, 4, 3, 4 }Output:3Explanation:

The maximum possible size would be 3 – {1 2 3} and {4 4 4}Input:a[] = { 2, 1, 5, 4, 3 }Output:1Explanation:

The maximum possible size would be 1 – {1} and {2}

**Approach:** To solve the problem mentioned above follow the steps given below:

- At first count, the frequency of all the numbers in the given array and also count the number of distinct elements in that array. Then count the maximum frequency among all of them
*maxfrq* - It is clear, that the maximum possible size of one array(that contains the same elements) would be maxfrq because it is the highest possible value of frequency of a number. And, the maximum possible size of another array(that contains all distinct elements) would be dist which is the total number of distinct elements. Now we have to consider 2 cases:
- If we take all the elements having frequency maxfrq in one array(that contains the same elements), then the total number of distinct elements left would be
*dist – 1*, so the other array can only contain dist – 1 number of elements. In this case, the answer would be,**ans1 = min(dist – 1, maxfrq)** - If we take all the elements having frequency maxfrq except one element in one array(that contains the same elements), then the total number of distinct elements left would be dist, so the other array can only contain dist number of elements. In this case, the answer would be,
**ans2 = min(dist, maxfrq – 1)**.

- If we take all the elements having frequency maxfrq in one array(that contains the same elements), then the total number of distinct elements left would be
- Hence, the actual answer is

max( min(dist – 1, maxfrq), min(dist, maxfrq – 1) )

- Below is the implementation of the above approach:

## C++

`// C++ implementation to Divide the array` `// into two arrays having same size,` `// one with distinct elements` `// and other with same elements` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the max size possible` `int` `findMaxSize(` `int` `a[], ` `int` `n)` `{` ` ` `vector<` `int` `> frq(n + 1);` ` ` `for` `(` `int` `i = 0; i < n; ++i)` ` ` `frq[a[i]]++;` ` ` `// Counting the maximum frequency` ` ` `int` `maxfrq` ` ` `= *max_element(` ` ` `frq.begin(), frq.end());` ` ` `// Counting total distinct elements` ` ` `int` `dist` ` ` `= n + 1 - count(` ` ` `frq.begin(),` ` ` `frq.end(), 0);` ` ` `int` `ans1 = min(maxfrq - 1, dist);` ` ` `int` `ans2 = min(maxfrq, dist - 1);` ` ` `// Find max of both the answer` ` ` `int` `ans = max(ans1, ans2);` ` ` `return` `ans;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `arr[] = { 4, 2, 4, 1, 4, 3, 4 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << findMaxSize(arr, n);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to Divide the array` `// into two arrays having same size,` `// one with distinct elements` `// and other with same elements` `import` `java.io.*; ` `import` `java.util.*; ` `class` `GFG { ` ` ` `// Function to find the max size possible` `static` `int` `findMaxSize(` `int` `a[], ` `int` `n)` `{` ` ` `ArrayList<Integer> frq = ` `new` `ArrayList<Integer>(n+` `1` `);` ` ` ` ` `for` `(` `int` `i = ` `0` `; i <= n; i++) ` ` ` `frq.add(` `0` `); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i)` ` ` `frq.set(a[i], frq.get(a[i]) + ` `1` `);` ` ` ` ` `// Counting the maximum frequency` ` ` `int` `maxfrq = Collections.max(frq);` ` ` `// Counting total distinct elements` ` ` `int` `dist = n + ` `1` `- Collections.frequency(frq, ` `0` `);` ` ` ` ` `int` `ans1 = Math.min(maxfrq - ` `1` `, dist);` ` ` `int` `ans2 = Math.min(maxfrq, dist - ` `1` `);` ` ` `// Find max of both the answer` ` ` `int` `ans = Math.max(ans1, ans2);` ` ` `return` `ans;` `}` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `4` `, ` `2` `, ` `4` `, ` `1` `, ` `4` `, ` `3` `, ` `4` `};` ` ` `int` `n = arr.length;` ` ` ` ` `System.out.println(findMaxSize(arr, n));` `} ` `} ` `// This code is contributed by coder001` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to divide the ` `# array into two arrays having same ` `# size, one with distinct elements ` `# and other with same elements ` `# Function to find the max size possible ` `def` `findMaxSize(a, n): ` ` ` `frq ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `) ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `frq[a[i]] ` `+` `=` `1` ` ` `# Counting the maximum frequency ` ` ` `maxfrq ` `=` `max` `(frq) ` ` ` ` ` `# Counting total distinct elements ` ` ` `dist ` `=` `n ` `+` `1` `-` `frq.count(` `0` `) ` ` ` `ans1 ` `=` `min` `(maxfrq ` `-` `1` `, dist) ` ` ` `ans2 ` `=` `min` `(maxfrq, dist ` `-` `1` `) ` ` ` `# Find max of both the answer ` ` ` `ans ` `=` `max` `(ans1, ans2) ` ` ` `return` `ans ` `# Driver code ` `arr ` `=` `[ ` `4` `, ` `2` `, ` `4` `, ` `1` `, ` `4` `, ` `3` `, ` `4` `] ` `n ` `=` `len` `(arr)` `print` `(findMaxSize(arr, n)) ` `# This code is contributed by divyamohan123` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to Divide the array` `// into two arrays having same size,` `// one with distinct elements` `// and other with same elements` `using` `System; ` `using` `System.Collections; ` `class` `GFG{ ` ` ` `// Function to find the max size possible` `static` `int` `findMaxSize(` `int` `[]a, ` `int` `n)` `{` ` ` `ArrayList frq = ` `new` `ArrayList(n + 1);` ` ` `for` `(` `int` `i = 0; i <= n; i++) ` ` ` `frq.Add(0); ` ` ` `for` `(` `int` `i = 0; i < n; ++i)` ` ` `frq[a[i]] = (` `int` `)frq[a[i]] + 1;` ` ` `// Counting the maximum frequency` ` ` `int` `maxfrq = ` `int` `.MinValue;` ` ` `for` `(` `int` `i = 0; i < frq.Count; i++)` ` ` `{` ` ` `if` `(maxfrq < (` `int` `)frq[i])` ` ` `{` ` ` `maxfrq = (` `int` `)frq[i];` ` ` `}` ` ` `}` ` ` `// Counting total distinct elements` ` ` `int` `dist = n + 1;` ` ` `for` `(` `int` `i = 0; i < frq.Count; i++)` ` ` `{` ` ` `if` `((` `int` `)frq[i] == 0)` ` ` `{` ` ` `dist--;` ` ` `}` ` ` `}` ` ` `int` `ans1 = Math.Min(maxfrq - 1, dist);` ` ` `int` `ans2 = Math.Min(maxfrq, dist - 1);` ` ` `// Find max of both the answer` ` ` `int` `ans = Math.Max(ans1, ans2);` ` ` `return` `ans;` `}` ` ` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `[]arr = {4, 2, 4, 1, 4, 3, 4};` ` ` `int` `n = arr.Length;` ` ` `Console.Write(findMaxSize(arr, n));` `} ` `} ` `// This code is contributed by Rutvik_56` |

*chevron_right*

*filter_none*

**Output:**

3

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:

- Closest pair in an Array such that one number is multiple of the other
- Check if an array contains only one distinct element
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Sub-strings that start and end with one character and have at least one other
- Check if every pair of 1 in the array is at least K length apart from each other
- Array formed using sum of absolute differences of that element with all other elements
- Subsequence pair from given Array having all unique and all same elements respectively
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Check if a given array contains duplicate elements within k distance from each other
- Maximum possible sum of a window in an array such that elements of same window in other array are unique
- Check if end of a sorted Array can be reached by repeated jumps of one more, one less or same number of indices as previous jump
- Convert a number of length N such that it contains any one digit at least 'K' times
- Divide every element of one array by other array elements
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Length of longest Fibonacci subarray formed by removing only one element
- Check if given number contains a digit which is the average of all other digits
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Find smallest subarray that contains all elements in same order
- Pair having all other given pairs lying between its minimum and maximum
- Largest number in given Array formed by repeatedly combining two same elements

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.