Length of array pair formed where one contains all distinct elements and other all same elements
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: 3
Explanation:
The maximum possible size would be 3 – {1 2 3} and {4 4 4}
Input: a[] = {2, 1, 5, 4, 3}
Output: 1
Explanation:
The maximum possible size would be 1 – {1} and {2}
Approach: To solve the problem mentioned above, follow the steps given below:
- 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 an 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 a 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 a dist number of elements. In this case, the answer would be, ans2 = min(dist, maxfrq – 1).
- Hence, the actual answer is
max( min(dist – 1, maxfrq), min(dist, maxfrq – 1) )
- Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxSize( int a[], int n)
{
vector< int > frq(n + 1);
for ( int i = 0; i < n; ++i)
frq[a[i]]++;
int maxfrq
= *max_element(
frq.begin(), frq.end());
int dist
= n + 1 - count(
frq.begin(),
frq.end(), 0);
int ans1 = min(maxfrq - 1, dist);
int ans2 = min(maxfrq, dist - 1);
int ans = max(ans1, ans2);
return ans;
}
int main()
{
int arr[] = { 4, 2, 4, 1, 4, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findMaxSize(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
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 );
int maxfrq = Collections.max(frq);
int dist = n + 1 - Collections.frequency(frq, 0 );
int ans1 = Math.min(maxfrq - 1 , dist);
int ans2 = Math.min(maxfrq, dist - 1 );
int ans = Math.max(ans1, ans2);
return ans;
}
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));
}
}
|
Python3
def findMaxSize(a, n):
frq = [ 0 ] * (n + 1 )
for i in range (n):
frq[a[i]] + = 1
maxfrq = max (frq)
dist = n + 1 - frq.count( 0 )
ans1 = min (maxfrq - 1 , dist)
ans2 = min (maxfrq, dist - 1 )
ans = max (ans1, ans2)
return ans
arr = [ 4 , 2 , 4 , 1 , 4 , 3 , 4 ]
n = len (arr)
print (findMaxSize(arr, n))
|
C#
using System;
using System.Collections;
class GFG{
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;
int maxfrq = int .MinValue;
for ( int i = 0; i < frq.Count; i++)
{
if (maxfrq < ( int )frq[i])
{
maxfrq = ( int )frq[i];
}
}
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);
int ans = Math.Max(ans1, ans2);
return ans;
}
public static void Main( string [] args)
{
int []arr = {4, 2, 4, 1, 4, 3, 4};
int n = arr.Length;
Console.Write(findMaxSize(arr, n));
}
}
|
Javascript
<script>
function findMaxSize(a, n)
{
var frq = Array(n + 1).fill(0);
for ( var i = 0; i < n; ++i)
frq[a[i]]++;
var maxfrq = frq.reduce((a,b)=>Math.max(a,b))
var dist
= n + 1 - frq.filter(x=> x ==0).length;
var ans1 = Math.min(maxfrq - 1, dist);
var ans2 = Math.min(maxfrq, dist - 1);
var ans = Math.max(ans1, ans2);
return ans;
}
var arr = [4, 2, 4, 1, 4, 3, 4];
var n = arr.length;
document.write( findMaxSize(arr, n));
</script>
|
Time Complexity: O(n) where n is the size of the given array.
Auxiliary Space: O(n) where n is the size of the given array.
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...