### Problem
Choose a subsequence of arr without reordering its elements. Every adjacent pair in the chosen sequence must increase by exactly difference, which may be negative or zero. Return its maximum length.
```cpp
class Solution {
public:
int longestSubsequence(vector<int>& arr, int difference);
};
```
### Examples
**Example 1:**
```text
Input: arr = [5,3,4,1], difference = -2
Output: 3
Explanation: The subsequence5,3,1 has the required step and retains input order.
```
**Example 2:**
```text
Input: arr = [2,2,1,2], difference = 0
Output: 3
Explanation: Select all three occurrences of2.
```
### Constraints
- The array contains 1 to 100000 integers.
- Both each value and difference lie in `[-10000,10000]`.
### Contract considerations
Return a subsequence length, with the original element order preserved.
```hint Read the boundary condition
When difference is zero or negative, must a valid subsequence still preserve the original element order?
```
### Boundary reminders
Return a subsequence length, with the original element order preserved.
Overview: Longest Ordered Subsequence With a Fixed Step. Choose a subsequence of arr without reordering its elements.
Choose a subsequence of arr without reordering its elements. Every adjacent pair in the chosen sequence must increase by exactly difference, which may be negative or zero. Return its maximum length.
class Solution {
public:
int longestSubsequence(vector<int>& arr, int difference);
};
Examples
Example 1:
Input: arr = [5,3,4,1], difference = -2
Output: 3
Explanation: The subsequence5,3,1 has the required step and retains input order.
Example 2:
Input: arr = [2,2,1,2], difference = 0
Output: 3
Explanation: Select all three occurrences of2.
Constraints
The array contains 1 to 100000 integers.
Both each value and difference lie in
[-10000,10000]
.
Contract considerations
Return a subsequence length, with the original element order preserved.
Boundary reminders
Return a subsequence length, with the original element order preserved.