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

📅  最后修改于: 2021-04-26 05:55:58             🧑  作者: 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
    // elment 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
    // elment 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
    # elment 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
    // elment 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


输出:
4



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