Editor

Repeats Close Together

#64 · Dictionaries and sets · Intermediate · 60 XP · about 10 min

Write has_close_repeat(nums, k) that returns True if the list holds two equal values at different positions i and j that are at most k apart (abs(i - j) <= k), and False otherwise.

Examples

has_close_repeat([1, 2, 3, 1], 3)  # returns True
has_close_repeat([1, 2, 3, 1], 2)  # returns False
has_close_repeat([1, 0, 1, 1], 1)  # returns True

In the first two examples the two 1s are 3 positions apart: close enough when k is 3, too far when k is 2.

Constraints

  • 0 <= len(nums) <= 100,000
  • 0 <= k <= 100,000
  • Values are integers

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.