Find the Longest Subsequence with Exact Value Spread One
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
### Problem
A harmonious subsequence has maximum value minus minimum value exactly one. Return the greatest length of such a subsequence of nums, or zero if none exists. Selected elements retain their original order but need not be adjacent.
```cpp
class Solution {
public:
int findLHS(vector<int>& nums);
};
```
### Examples
**Example 1:**
```text
nums = [4,5,4,9,5,5]
Output: 5
Explanation: Keep all occurrences of 4 and 5.
```
**Example 2:**
```text
nums = [-2,-2,-2]
Output: 0
Explanation: A spread of zero does not qualify.
```
### Constraints
- Input size: 1..20000 integers, with values in -1000000000..1000000000.
### Clarifying Questions
Require both consecutive values and retain every eligible occurrence in their original order.
```hint Read the boundary condition
Which integer values can occur when maximum minus minimum equals one?
```
### Contract considerations
Require both consecutive values and retain every eligible occurrence in their original order.
Overview: Find the Longest Subsequence with Exact Value Spread One. A harmonious subsequence has maximum value minus minimum value exactly one.
Find the Longest Subsequence with Exact Value Spread One
Amazon
Sep 12, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0
Problem
A harmonious subsequence has maximum value minus minimum value exactly one. Return the greatest length of such a subsequence of nums, or zero if none exists. Selected elements retain their original order but need not be adjacent.
class Solution {
public:
int findLHS(vector<int>& nums);
};
Examples
Example 1:
nums = [4,5,4,9,5,5]
Output: 5
Explanation: Keep all occurrences of 4 and 5.
Example 2:
nums = [-2,-2,-2]
Output: 0
Explanation: A spread of zero does not qualify.
Constraints
Input size: 1..20000 integers, with values in -1000000000..1000000000.
Clarifying Questions Guidance
Require both consecutive values and retain every eligible occurrence in their original order.
Contract considerations
Require both consecutive values and retain every eligible occurrence in their original order.