📌  相关文章
📜  来自给定 Array 的子序列对分别具有所有唯一元素和所有相同元素

📅  最后修改于: 2021-09-07 04:06:46             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是选择两个长度相等的子序列,使得第一个子序列必须具有所有唯一元素,而第二个子序列必须具有所有相同元素。打印子序列对的最大长度。

例子:

方法:

  1. 计算给定数组中元素的最大频率(比如f )。
  2. 计算数组中存在的不同元素(比如d )。
  3. 要使两个具有给定属性的子序列长度相等,则第一个子序列的大小不能超过d ,第二个子序列的大小不能超过f
  4. 子序列的最大值基于以下两种情况给出:
    • 具有不同元素的子序列必须具有最大频率的元素。因此, (d, f – 1)的最小值是具有给定属性的子序列的可能长度。
    • 如果具有不同元素的子序列的长度大于最大频率,则(d – 1, f)的最小值是具有给定属性的子序列的可能长度。
  5. 以上两种情况的最大值是具有给定属性的子序列的最大长度。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum length
// of subsequences with given property
int maximumSubsequence(int arr[], int N)
{
    // To store the frequency
    unordered_map M;
 
    // Traverse the array to store the
    // frequency
    for (int i = 0; i < N; i++) {
        M[arr[i]]++;
    }
 
    // M.size() given count of distinct
    // element in arr[]
    int distinct_size = M.size();
    int maxFreq = 1;
 
    // Traverse map to find max frequency
    for (auto& it : M) {
 
        maxFreq = max(maxFreq, it.second);
    }
 
    // Find the maximum length on the basis
    // of two cases in the approach
 
    cout << max(min(distinct_size, maxFreq - 1),
                min(distinct_size - 1, maxFreq));
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 4, 4, 4, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    maximumSubsequence(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum length
// of subsequences with given property
static void maximumSubsequence(int arr[], int N)
{
     
    // To store the frequency
    HashMap M = new HashMap();
 
    // Traverse the array to store the
    // frequency
    for(int i = 0; i < N; i++)
    {
        if(M.containsKey(arr[i]))
        {
            M.put(arr[i], M.get(arr[i]) + 1);
        }
        else
        {
            M.put(arr[i], 1);
        }
    }
 
    // M.size() given count of distinct
    // element in arr[]
    int distinct_size = M.size();
    int maxFreq = 1;
 
    // Traverse map to find max frequency
    for(Map.Entry it : M.entrySet())
    {
        maxFreq = Math.max(maxFreq, it.getValue());
    }
 
    // Find the maximum length on the basis
    // of two cases in the approach
 
    System.out.print(Math.max(
        Math.min(distinct_size, maxFreq - 1),
        Math.min(distinct_size - 1, maxFreq)));
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 4, 4, 4, 4, 5 };
    int N = arr.length;
 
    // Function call
    maximumSubsequence(arr, N);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python 3 program for the above approach
 
# Function to find the maximum length
# of subsequences with given property
def maximumSubsequence(arr, N):
     
    # To store the frequency
    M = {i : 0 for i in range(100)}
 
    # Traverse the array to store the
    # frequency
    for i in range(N):
        M[arr[i]] += 1
 
    # M.size() given count of distinct
    # element in arr[]
    distinct_size = len(M)
    maxFreq = 1
 
    # Traverse map to find max frequency
    for value in M.values():
        maxFreq = max(maxFreq, value)
 
    # Find the maximum length on the basis
    # of two cases in the approach
 
    print(max(min(distinct_size,  maxFreq - 1),
          min(distinct_size - 1, maxFreq)))
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3, 4, 4, 4, 4, 5 ]
    N = len(arr)
 
    # Function call
    maximumSubsequence(arr, N)
 
# This code is contributed by Samarth


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to find the maximum length
// of subsequences with given property
static void maximumSubsequence(int []arr, int N)
{
     
    // To store the frequency
    Dictionary M = new Dictionary();
 
    // Traverse the array to store the
    // frequency
    for(int i = 0; i < N; i++)
    {
        if(M.ContainsKey(arr[i]))
        {
            M[arr[i]] =  M[arr[i]] + 1;
        }
        else
        {
            M.Add(arr[i], 1);
        }
    }
 
    // M.Count given count of distinct
    // element in []arr
    int distinct_size = M.Count;
    int maxFreq = 1;
 
    // Traverse map to find max frequency
    foreach(KeyValuePair m in M)
    {
        maxFreq = Math.Max(maxFreq, m.Value);
    }
 
    // Find the maximum length on the basis
    // of two cases in the approach
 
    Console.Write(Math.Max(
        Math.Min(distinct_size, maxFreq - 1),
        Math.Min(distinct_size - 1, maxFreq)));
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4, 4, 4, 4, 4, 5 };
    int N = arr.Length;
 
    // Function call
    maximumSubsequence(arr, N);
}
}
 
// This code is contributed by Rohit_ranjan


Javascript


输出:
4

时间复杂度: O(N) ,其中 N 是数组中的元素数。
辅助空间复杂度: O(N) ,其中 N 是数组中的元素数。

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