Return True if there are two different positions i and j with nums[i] == nums[j] and abs(i - j) <= k. Otherwise return False.
The starter code compares every pair. It is correct but too slow. Make it fast.
Example 1
Input:
contains_nearby_duplicate([1, 2, 3, 1], 3)
Output:
True
The two 1s are 3 positions apart.
Example 2
Input:
contains_nearby_duplicate([1, 2, 3, 1, 2, 3], 2)
Output:
False
Equal values are always 3 positions apart, more than 2.
0 <= len(nums) <= 2 * 10**50 <= k <= 2 * 10**5-10**9 <= nums[i] <= 10**9No results yet
Run your code to check it against the examples, or submit to run every test.