LeetCode 594, Easy. Topics: Array, Hash Table, Sliding Window, Sorting, Counting. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:
Problem
We define a harmonious array as an array where the difference between its maximum value and its minimum value is exactly 1.
Given an integer array nums, return the length of its longest harmonious subsequence among all its possible subsequences.
Examples
Constraints
- 1 <= nums.length <= 2 * 10^4
- -10^9 <= nums[i] <= 10^9
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026