# 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 first subsequence must have all the unique elements and second subsequence must have all the same element. Print the maximum length of the subsequence pair.

**Examples:**

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

Output:3

Explanation:

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:2

Explanation:

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 size of first subsequence cannot exceed
**d**and size of second subsequence cannot exceeds**f**. - The maximum value of subsequence is given on the basis of below two cases:
- The subsequence with distinct elements must have an element with maximum frequency. Therefore minimum of
**(d, f – 1)**is the possible length of subsequence with given property. - If the length of a subsequence with distinct element is greater than maximum frequency then, the minimum of
**(d – 1, f)**is the possible length of subsequence with given property.

- The subsequence with distinct elements must have an element with maximum frequency. Therefore minimum of
- The maximum value in the above two cases is the maximum length of 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 ` ` ` `// 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; ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# 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 ` |

*chevron_right*

*filter_none*

**Output:**

4

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

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

**Auxiliary Space Complexity:**O(N)Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find Unique pair in an array with pairs of numbers
- Length of array pair formed where one contains all distinct elements and other all same elements
- Find sum of all unique elements in the array for K queries
- Unique element in an array where all elements occur k times except one
- Unique element in an array where all elements occur K times except one | Set 2
- Shortest subarray to be removed to make all Array elements unique
- Divide the array into minimum number of sub-arrays having unique elements
- Rearrange an array to minimize sum of product of consecutive pair elements
- Minimum number of elements which are not part of Increasing or decreasing subsequence in array
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Maximum possible sum of a window in an array such that elements of same window in other array are unique
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Longest subsequence such that absolute difference between every pair is atmost 1
- Minimum length subarray containing all unique elements after Q operations
- Maximum sum subsequence with at-least k distant elements
- Maximum subsequence sum such that all elements are K distance apart
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Find Sum of all unique sub-array sum for a given array.
- Longest subsequence with first and last element greater than all other elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.