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}.

**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**.

- Assign
- 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**. - Maximize count of Decreasing Subsequences from the given Array
- Count the number of contiguous increasing and decreasing subsequences in a sequence
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Remove minimum elements from array such that no three consecutive element are either increasing or decreasing
- Maximize length of Non-Decreasing Subsequence by reversing at most one Subarray
- Maximize product of same-indexed elements of same size subsequences
- Maximize sum of consecutive differences in a circular array
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Count of subsequences in an array with sum less than or equal to X
- Count of possible subarrays and subsequences using given length of Array
- Count of subsequences from a given Array having Binary Equivalence
- Count subsequences which contains both the maximum and minimum array element
- Maximize the maximum among minimum of K consecutive sub-arrays
- Maximize distance between any two consecutive 1's after flipping M 0's
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Maximize product of digit sum of consecutive pairs in a subsequence of length K
- Find the non decreasing order array from given array
- Check if it is possible to make array increasing or decreasing by rotating the array
- Rearrange array to make it non-decreasing by swapping pairs having GCD equal to minimum array element

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<Integer, Integer> 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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

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.