Skip to content

Practice Longest Harmonious Subsequence

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#594
LeetCode Problem

Longest Harmonious Subsequence

Problem Statement

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.

Source: Longest Harmonious Subsequence on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,3,2,2,5,2,3,7]
Output
5

# Example 2

Input
nums = [1,2,3,4]
Output
2

# Example 3

Input
nums = [1,1,1,1]
Output
0

Constraints

1 <= nums.length <= 2 * 10^4
-10^9 <= nums[i] <= 10^9

Related LeetCode Problems

Practice more problems similar to Longest Harmonious Subsequence.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.