FunctionRacerProblemsRace

Close Neighbors

BeginnerArraysPython, JavaScript, TypeScript, Java, C++

Two items standing next to each other in nums (positions i and i + 1) are close when the absolute difference between them is at most d.

Return how many close pairs nums has. A list with fewer than two items has no pairs, so it gives 0.

Example 1

nums [1,2,10,11,13]
d 2
returns 3

The neighbor pairs differ by 1, 8, 1 and 2. Three of them are at most 2 apart.

Example 2

nums [5,50,500]
d 10
returns 0

The neighbors differ by 45 and 450, both far more than 10.

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 Arrays problems