PIXELBANKv9.1.0
Menu

Given an array and integer k, return True if there are two distinct indices i and j such that nums[i] == nums[j] and abs(i - j) <= k.

Example:

Input:
1,2,3,1
3
Output:
True
Reasoning:
  • The input array is [1, 2, 3, 1] and the integer k is 33.
  • We iterate through the array to find duplicate elements within a distance of kk indices.
  • At index 00, the value is 11, and at index 33, the value is also 11, with an index difference of abs(0−3)=3abs(0-3) = 3, which satisfies the condition abs(i−j)≤kabs(i - j) \leq k.
  • Since a duplicate is found within the specified distance, the function returns True.

Constraints:

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 0 <= k <= 10^5
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Contains Duplicate II - Easy | PixelBank