Close Neighbors
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
0 <= len(nums) <= 100-1000 <= nums[i] <= 10000 <= d <= 2000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Concatenation of ArrayBeginner
- Deal Into PilesBeginner
- Decompress a Pair ListBeginner
- Find the Highest AltitudeBeginner
- Groups of KBeginner
- Items Within BudgetBeginner
- Kids With the Most CandiesBeginner
- Largest At Least Twice OthersBeginner