📌  相关文章
📜  形成的数组对的长度,其中一个包含所有不同的元素和其他所有相同的元素

📅  最后修改于: 2021-09-03 13:56:12             🧑  作者: Mango

给定一个数组arr[] ,任务是确定可以使用给定数组元素创建的两个相同大小的数组的最大可能大小,以便在一个数组中所有元素都是不同的,而在另一个数组中所有元素是一样的
注意:使用给定数组的所有元素来构建两个数组不是强制性的。

例子:

方法:解决上述问题,请按照以下步骤操作:

  • 首先,计算给定数组中所有数字的频率,并计算该数组中不同元素的数量。然后计算所有这些中的最大频率maxfrq
  • 很明显,数组(包含相同元素)的最大可能大小将是 maxfrq,因为它是数字频率的最高可能值。并且,另一个数组(包含所有不同元素)的最大可能大小将是 dist,这是不同元素的总数。现在我们必须考虑两种情况:
    1. 如果我们在一个数组中取所有频率为 maxfrq 的元素(包含相同的元素),那么剩下的不同元素的总数将为dist – 1 ,因此另一个数组只能包含 dist – 1 个元素。在这种情况下,答案是ans1 = min(dist – 1, maxfrq)
    2. 如果我们取除一个数组中的一个元素(包含相同元素)之外的所有频率为 maxfrq 的元素,那么剩下的不同元素的总数将为 dist,因此另一个数组只能包含 dist 个元素。在这种情况下,答案是ans2 = 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


Javascript


输出:
3

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live