给定一个数组arr [] ,任务是确定可以使用给定数组元素创建的相同大小的两个数组的最大可能大小,以便在一个数组中所有元素都是不同的,而在另一个数组中,所有元素都是一样的。
注意:使用给定数组的所有元素来构建两个数组不是强制性的。
例子:
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}
方法:要解决上述问题,请执行以下步骤:
- 首次计数时,将计算给定数组中所有数字的频率,并计算该数组中不同元素的数量。然后计算所有频率中的最大频率maxfrq
- 显然,一个数组(包含相同元素)的最大可能大小为maxfrq,因为它是数字频率的最大可能值。并且,另一个数组(包含所有不同元素)的最大可能大小为dist,它是不同元素的总数。现在我们要考虑2种情况:
- 如果我们在一个数组中包含所有频率为maxfrq的元素(包含相同的元素),那么剩下的不同元素的总数将为dist – 1 ,因此另一个数组只能包含dist – 1个元素。在这种情况下,答案将是: ans1 = min(dist – 1,maxfrq)
- 如果我们将一个数组中的一个元素(包含相同的元素)以外的所有频率为maxfrq的元素都采用,那么剩下的不同元素的总数将是dist,因此另一个数组只能包含dist个元素。在这种情况下,答案将是ans2 = min(dist,maxfrq – 1) 。
- 因此,实际答案是
max( min(dist – 1, maxfrq), min(dist, maxfrq – 1) )
- 下面是上述方法的实现:
C++
// C++ implementation to Divide the array
// into two arrays having same size,
// one with distinct elements
// and other with same elements
#include
using namespace std;
// Function to find the max size possible
int findMaxSize(int a[], int n)
{
vector 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;
}
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 frq = new ArrayList(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
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
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
输出:
3