Given an array arr[] consisting of N integers, the task is to find the maximum count of decreasing subsequences possible from an array that satisfies the following conditions:
- Each subsequence is in its longest possible form.
- The difference between adjacent elements of the subsequence is always 1.
Examples:
Input: arr[] = {2, 1, 5, 4, 3}
Output: 2
Explanation:
Possible decreasing subsequences are { 5, 4, 3 } and { 2, 1 }.
Input: arr[] = {4, 5, 2, 1, 4}
Output: 3
Explanation:
Possible decreasing subsequences are { 4 }, { 5, 4} and { 2, 1}.
Approach:
The idea is to use a HashMap to solve the problem. Follow the steps below:
- Maintain a HashMap to store the count of subsequences possible for an array element and maxSubsequences to count the total number of possible subsequences.
- Traverse the array, and for each element arr[i], check if any subsequence exists which can have arr[i] as the next element, by the count assigned to arr[i] in the HashMap.
- If exists, do the following:
- Assign arr[i] as the next element of the subsequence.
- Decrease count assigned to arr[i] in the HashMap, as the number of possible subsequences with arr[i] as the next element has decreased by 1.
- Similarly, increase count assigned to arr[i] – 1 in the HashMap, as the number of possible subsequences with arr[i] – 1 as the next element has increased by 1.
- Otherwise, increase maxCount, as a new subsequence is required and repeat the above step to modify the HashMap.
- After completing the traversal of the array, print the value of maxCount.
C++
#include <bits/stdc++.h>
using namespace std;
int maxSubsequences( int arr[], int n)
{
unordered_map< int , int > m;
int maxCount = 0;
int count;
for ( int i = 0; i < n; i++) {
if (m.find(arr[i]) != m.end()) {
count = m[arr[i]];
if (count > 1) {
m[arr[i]] = count - 1;
}
else
m.erase(arr[i]);
if (arr[i] - 1 > 0)
m[arr[i] - 1] += 1;
}
else {
maxCount++;
if (arr[i] - 1 > 0)
m[arr[i] - 1] += 1;
}
}
return maxCount;
}
int main()
{
int n = 5;
int arr[] = { 4, 5, 2, 1, 4 };
cout << maxSubsequences(arr, n) << endl;
}
|
Java
import java.util.*;
class GFG {
static int maxSubsequences( int arr[], int n)
{
HashMap<Integer, Integer> map
= new HashMap<>();
int maxCount = 0 ;
int count;
for ( int i = 0 ; i < n; i++)
{
if (map.containsKey(arr[i]))
{
count = map.get(arr[i]);
if (count > 1 )
{
map.put(arr[i], count - 1 );
}
else
map.remove(arr[i]);
if (arr[i] - 1 > 0 )
map.put(arr[i] - 1 ,
map.getOrDefault(arr[i] - 1 , 0 ) + 1 );
}
else {
maxCount++;
if (arr[i] - 1 > 0 )
map.put(arr[i] - 1 ,
map.getOrDefault(arr[i] - 1 , 0 ) + 1 );
}
}
return maxCount;
}
public static void main(String[] args)
{
int n = 5 ;
int arr[] = { 4 , 5 , 2 , 1 , 4 };
System.out.println(maxSubsequences(arr, n));
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maxSubsequences( int []arr, int n)
{
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
int maxCount = 0;
int count;
for ( int i = 0; i < n; i++)
{
if (map.ContainsKey(arr[i]))
{
count = map[arr[i]];
if (count > 1)
{
map.Add(arr[i], count - 1);
}
else
map.Remove(arr[i]);
if (arr[i] - 1 > 0)
if (map.ContainsKey(arr[i] - 1))
map[arr[i] - 1]++;
else
map.Add(arr[i] - 1, 1);
}
else
{
maxCount++;
if (arr[i] - 1 > 0)
if (map.ContainsKey(arr[i] - 1))
map[arr[i] - 1]++;
else
map.Add(arr[i] - 1, 1);
}
}
return maxCount;
}
public static void Main(String[] args)
{
int n = 5;
int []arr = { 4, 5, 2, 1, 4 };
Console.WriteLine(maxSubsequences(arr, n));
}
}
|
Python3
from collections import defaultdict
def maxSubsequences(arr, n) - > int :
m = defaultdict( int )
maxCount = 0
count = 0
for i in range ( 0 , n):
if arr[i] in m.keys():
count = m[arr[i]]
if count > 1 :
m[arr[i]] = count - 1
else :
m.pop(arr[i])
if arr[i] - 1 > 0 :
m[arr[i] - 1 ] + = 1
else :
maxCount + = 1
if arr[i] - 1 > 0 :
m[arr[i] - 1 ] + = 1
return maxCount
if __name__ = = '__main__' :
n = 5
arr = [ 4 , 5 , 2 , 1 , 4 ]
print (maxSubsequences(arr, n))
|
Javascript
<script>
function maxSubsequences(arr, n)
{
let map = new Map();
let maxCount = 0;
let count;
for (let i = 0; i < n; i++)
{
if (map.has(arr[i]))
{
count = map[arr[i]];
if (count > 1)
{
map.add(arr[i], count - 1);
}
else
map. delete (arr[i]);
if (arr[i] - 1 > 0)
if (map.has(arr[i] - 1))
map[arr[i] - 1]++;
else
map.set(arr[i] - 1, 1);
}
else
{
maxCount++;
if (arr[i] - 1 > 0)
if (map.has(arr[i] - 1))
map[arr[i] - 1]++;
else
map.set(arr[i] - 1, 1);
}
}
return maxCount;
}
let n = 5;
let arr = [ 4, 5, 2, 1, 4 ];
document.write(maxSubsequences(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
29 Nov, 2022
Like Article
Save Article