FunctionRacerProblemsRace

Longest Harmonious Subsequence

EasyHash mapsPython, JavaScript, TypeScript, Java, C++

Pick any items of nums (they need not be neighbours, and the order does not matter here). The chosen group is harmonious if its biggest value minus its smallest value is exactly 1.

Return the largest size of a harmonious group, or 0 if none exists.

Example 1

nums [1,3,2,2,5,2,3,7]
returns 5

Take the three 2s and two 3s: size 5.

Example 2

nums [4,4,4]
returns 0

The difference would be 0, never 1.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Hash maps problems