Maximize count of Decreasing Subsequences from the given Array
Last Updated :
05 Sep, 2022
Given an array arr[], the task is to rearrange the array to generate maximum decreasing subsequences and print the count of the maximum number of subsequences possible such that each array element can be part of a single subsequence and the length of the subsequences needs to be maximized.
Example:
Input: arr[] = {5, 2, 3, 4}
Output: 1
Explanation:
The given array can be rearranged to {5, 4, 3, 2}.
Since every element is occurring only once, the rearranged array forms a decreasing subsequence of maximum possible length.
Input: arr[] = {5, 2, 6, 5, 2, 4, 5, 2}
Output: 3
Explanation:
The given array can be rearranged to {5, 2, 6, 5, 4, 2, 5, 2}.
The 3 decreasing subsequences of maximum possible length possible from the given array are {6, 5, 4, 2}, {5, 2} and {5, 2}
Approach:
To solve the problem, there is no need to actually rearrange the given array. Since every element can be part of a single subsequence, the number of subsequences will be equal to the frequency of the most frequent element.
Follow the steps below to solve the problem:
- Traverse the array and store the frequency of each array element in a Hashmap
- Now, traverse the HashMap to find the maximum frequency of an element in an array.
- Print the maximum frequency as the count of maximum decreasing subsequences possible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Maximum_subsequence( int A[], int N)
{
unordered_map< int , int > frequency;
int max_freq = 0;
for ( int i = 0; i < N; i++) {
frequency[A[i]]++;
}
for ( auto it : frequency) {
if (it.second > max_freq) {
max_freq = it.second;
}
}
cout << max_freq << endl;
}
int main()
{
int arr[] = { 5, 2, 6, 5, 2, 4, 5, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
Maximum_subsequence(arr, N);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void Maximum_subsequence(
int [] A, int N)
{
HashMap<Integer, Integer> frequency
= new HashMap<>();
int max_freq = 0 ;
for ( int i = 0 ; i < N; i++) {
if (frequency.containsKey(A[i])) {
frequency.replace(A[i],
frequency.get(A[i]) + 1 );
}
else {
frequency.put(A[i], 1 );
}
}
for (Map.Entry it : frequency.entrySet()) {
if (( int )it.getValue() > max_freq) {
max_freq = ( int )it.getValue();
}
}
System.out.println(max_freq);
}
public static void main(String[] args)
{
int arr[] = { 5 , 2 , 6 , 5 , 2 , 4 , 5 , 2 };
int N = arr.length;
Maximum_subsequence(arr, N);
}
}
|
Python3
def Maximum_subsequence(A, N):
frequency = dict ();
max_freq = 0 ;
for i in range (N):
if (A[i] in frequency):
frequency[A[i]] + = 1
else :
frequency[A[i]] = 1
for it in frequency:
if (frequency[it] > max_freq):
max_freq = frequency[it];
print (max_freq);
arr = [ 5 , 2 , 6 , 5 , 2 , 4 , 5 , 2 ];
Maximum_subsequence(arr, len (arr));
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void Maximum_subsequence( int [] A,
int N)
{
Dictionary< int ,
int > frequency = new Dictionary< int ,
int >();
int max_freq = 0;
for ( int i = 0; i < N; i++)
{
if (frequency.ContainsKey(A[i]))
{
frequency[A[i]] = frequency[A[i]] + 1;
}
else
{
frequency.Add(A[i], 1);
}
}
foreach (KeyValuePair< int ,
int > it in frequency)
{
if (( int )it.Value > max_freq)
{
max_freq = ( int )it.Value;
}
}
Console.WriteLine(max_freq);
}
public static void Main(String[] args)
{
int []arr = { 5, 2, 6, 5, 2, 4, 5, 2 };
int N = arr.Length;
Maximum_subsequence(arr, N);
}
}
|
Javascript
<script>
function Maximum_subsequence(A, N)
{
var frequency = new Map();
var max_freq = 0;
for ( var i = 0; i < N; i++) {
if (frequency.has(A[i]))
frequency.set(A[i], frequency.get(A[i])+1)
else
frequency.set(A[i], 1);
}
frequency.forEach((value, key) => {
if (value > max_freq) {
max_freq = value;
}
});
document.write( max_freq );
}
var arr = [5, 2, 6, 5, 2, 4, 5, 2];
var N = arr.length;
Maximum_subsequence(arr, N);
</script>
|
Time Complexity: O(N), since one traversal of the array is required to complete all operations.
Auxiliary Space: O(N), an unordered map is used and in the worst case, all elements will be stored inside it.
Share your thoughts in the comments
Please Login to comment...