# Maximize count of Decreasing Consecutive Subsequences from an Array

Given an array arr[] consisting of N integers, the task is to find the maximum count of decreasing subsequences possible from an array which 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}.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.
• Below is the implementation of the above approach:

## Java

 `// Java program to implememt ` `// the above approach ` `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to find the maximum number ` `    ``// number of required subsequences ` `    ``static` `int` `maxSubsequences(``int` `arr[], ``int` `n) ` `    ``{ ` ` `  `        ``// HashMap to store number of ` `        ``// arrows available with ` `        ``// height of arrow as key ` `        ``HashMap map ` `            ``= ``new` `HashMap<>(); ` ` `  `        ``// Stores the maximum count ` `        ``// of possible subsequences ` `        ``int` `maxCount = ``0``; ` ` `  `        ``// Stores the count of ` `        ``// possible subsequences ` `        ``int` `count; ` ` `  `        ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `            ``// Check if i-th element can be ` `            ``// part of any of the previous ` `            ``// subsequence ` `            ``if` `(map.containsKey(arr[i])) { ` ` `  `                ``// Count  of subsequences ` `                ``// possible with arr[i] as ` `                ``// the next element ` `                ``count = map.get(arr[i]); ` ` `  `                ``// If more than one such ` `                ``// subsequence exists ` `                ``if` `(count > ``1``) { ` ` `  `                    ``// Include arr[i] in a subsequence ` `                    ``map.put(arr[i], count - ``1``); ` `                ``} ` ` `  `                ``// Otherwise ` `                ``else` `                    ``map.remove(arr[i]); ` ` `  `                ``// Increase count of subsequence possible ` `                ``// with arr[i] - 1 as the next element ` `                ``if` `(arr[i] - ``1` `> ``0``) ` `                    ``map.put(arr[i] - ``1``, ` `                            ``map.getOrDefault(arr[i] - ``1``, ``0``) + ``1``); ` `            ``} ` `            ``else` `{ ` ` `  `                ``// Start a new subsequence ` `                ``maxCount++; ` ` `  `                ``// Increase count of subsequence possible ` `                ``// with arr[i] - 1 as the next element ` `                ``if` `(arr[i] - ``1` `> ``0``) ` `                    ``map.put(arr[i] - ``1``, ` `                            ``map.getOrDefault(arr[i] - ``1``, ``0``) + ``1``); ` `            ``} ` `        ``} ` ` `  `        ``// Return the answer ` `        ``return` `maxCount; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``8``; ` `        ``int` `arr[] = { ``4``, ``3``, ``4``, ``2``, ``3``, ``4``, ``2``, ``2` `}; ` `        ``System.out.println(maxSubsequences(arr, n)); ` `    ``} ` `} `

Output:

```4
```

Time Complexity: O(N)
Auxiliary Space: O(1)

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Improved By : nidhi_biet

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.