# Subsequence pair from given Array having all unique and all same elements respectively

Given an array **arr[]** of **N** integers, the task is to choose the two subsequences of equal lengths such that the first subsequence must have all the unique elements and the second subsequence must have all the same elements. Print the maximum length of the subsequence pair.

**Examples:**

Input:arr[] = {1, 2, 3, 1, 2, 3, 3, 3}Output:3Explanation:

The first subsequence consists of elements {1, 2, 3}.

The second subsequence consists of elements {3, 3, 3}.

Input:arr[] = {2, 2, 2, 3}Output:2Explanation:

The first subsequence consists of elements {2, 3}.

The second subsequence consists of elements {2, 2}.

**Approach:**

- Count the maximum frequency(say
**f**) of the element in the given array. - Count the distinct elements(say
**d**) present in the array. - To make both the subsequence of equal length with the given property, then the size of the first subsequence cannot exceed
**d**, and the size of the second subsequence cannot exceed**f**. - The maximum value of the subsequence is given on the basis of two cases below:
- The subsequence with distinct elements must have an element with maximum frequency. Therefore, the minimum of
**(d, f – 1)**is the possible length of a subsequence with the given property. - If the length of a subsequence with a distinct element is greater than maximum frequency, then, the minimum of
**(d – 1, f)**is the possible length of a subsequence with the given property.

- The subsequence with distinct elements must have an element with maximum frequency. Therefore, the minimum of
- The maximum value in the above two cases is the maximum length of a subsequence with the given property.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `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<` `int` `, ` `int` `> 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<Integer,` ` ` `Integer> M = ` `new` `HashMap<Integer,` ` ` `Integer>();` ` ` `// 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<Integer, Integer> 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<` `int` `,` ` ` `int` `> M = ` `new` `Dictionary<` `int` `,` ` ` `int` `>();` ` ` `// 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<` `int` `, ` `int` `> 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

`<script>` `// Javascript program for the above approach` `// Function to find the maximum length` `// of subsequences with given property` `function` `maximumSubsequence(arr, N) {` ` ` `// To store the frequency` ` ` `let M = ` `new` `Map();` ` ` `// Traverse the array to store the` ` ` `// frequency` ` ` `for` `(let i = 0; i < N; i++) {` ` ` `if` `(M.has(arr[i])) {` ` ` `M.set(arr[i], M.get(arr[i]) + 1);` ` ` `}` ` ` `else` `{` ` ` `M.set(arr[i], 1);` ` ` `}` ` ` `}` ` ` `// M.size() given count of distinct` ` ` `// element in arr[]` ` ` `let distinct_size = M.size;` ` ` `let maxFreq = 1;` ` ` `// Traverse map to find max frequency` ` ` `for` `(let it of M) {` ` ` `maxFreq = Math.max(maxFreq, it[1]);` ` ` `}` ` ` `// Find the maximum length on the basis` ` ` `// of two cases in the approach` ` ` `document.write(Math.max(Math.min(distinct_size, maxFreq - 1), Math.min(distinct_size - 1, maxFreq)));` `}` `// Driver Code` `let arr = [1, 2, 3, 4, 4, 4, 4, 4, 5];` `let N = arr.length;` `// Function call` `maximumSubsequence(arr, N);` `// This code is contributed by _saurabh_jaiswal` `</script>` |

**Output:**

4

* Time Complexity: O(N)*, where N is the number of elements in the array.

*, where N is the number of elements in the array.*

**Auxiliary Space Complexity:**O(N)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.