Longest Harmonious Subsequence - Problem

A harmonious array is a special type of array where the difference between its maximum and minimum values is exactly 1. This creates a balanced, "harmonious" relationship between the elements.

Given an integer array nums, your task is to find the length of the longest harmonious subsequence among all possible subsequences.

A subsequence is derived from an array by deleting some or no elements without changing the order of the remaining elements. However, for this problem, order doesn't matter since we only care about the min/max difference.

Example: In array [1,3,2,2,5,2,3,7], one harmonious subsequence could be [3,2,2,2,3] (min=2, max=3, difference=1), which has length 5.

Goal: Return the length of the longest possible harmonious subsequence, or 0 if no harmonious subsequence exists.

Input & Output

example_1.py โ€” Basic Harmonious Array
$ Input: [1, 3, 2, 2, 5, 2, 3, 7]
โ€บ Output: 5
๐Ÿ’ก Note: The longest harmonious subsequence is [3, 2, 2, 2, 3] with length 5. The minimum value is 2, maximum is 3, and their difference is exactly 1, making it harmonious.
example_2.py โ€” Single Element Types
$ Input: [1, 2, 3, 4]
โ€บ Output: 2
๐Ÿ’ก Note: Multiple harmonious subsequences exist: [1,2], [2,3], [3,4]. Each has length 2, so we return 2 as the maximum length.
example_3.py โ€” No Harmonious Subsequence
$ Input: [1, 1, 1, 1]
โ€บ Output: 0
๐Ÿ’ก Note: All elements are identical, so max - min = 1 - 1 = 0, which doesn't satisfy the harmony condition (difference must be exactly 1). No harmonious subsequence exists.

Visualization

Tap to expand
๐ŸŽต Musical Harmony DetectionSong Ratings: [1โญ, 3โญ, 2โญ, 2โญ, 5โญ, 2โญ, 3โญ, 7โญ]Goal: Find longest harmonious playlist (ratings differ by exactly 1)๐ŸŽผ Rating Frequency Chart:1โญ1song2โญ3songs3โญ2songs5โญ1song7โญ1song๐ŸŽฏ Harmony Detection:๐Ÿ” Check 1โญ โ†’ 2โญ: Adjacent ratings! Playlist length = 1 + 3 = 4 songs๐ŸŽต Check 2โญ โ†’ 3โญ: Adjacent ratings! Playlist length = 3 + 2 = 5 songs โœจ๐Ÿ” Check 3โญ โ†’ 4โญ: No 4โญ songs available๐Ÿ” Check 5โญ โ†’ 6โญ: No 6โญ songs available๐Ÿ” Check 7โญ โ†’ 8โญ: No 8โญ songs available๐Ÿ† Longest Harmonious Playlist:5 songs (2โญ + 3โญ ratings)Perfect harmony: max - min = 3 - 2 = 1 โœ“
Understanding the Visualization
1
Catalog Songs
Count how many songs we have at each rating level
2
Find Harmony
Look for adjacent ratings that can create harmonious playlists
3
Calculate Length
Sum the counts of harmonious rating pairs
4
Pick Longest
Choose the harmonious playlist with maximum songs
Key Takeaway
๐ŸŽฏ Key Insight: Only consecutive integers can form harmonious arrays, so we just need to count frequencies and check adjacent number pairs - no need to examine all possible subsequences!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through array with O(1) hash operations

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash map stores at most n unique elements with their frequencies

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 2 ร— 104
  • -109 โ‰ค nums[i] โ‰ค 109
  • The array can contain duplicate elements
  • Subsequence order doesn't matter (we only care about min/max difference)
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 25
38.5K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen