Question is : Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k
Ex 1: Input: nums = [1,2,3,1], k = 3 Output: true
My solution is
def containsNearbyDuplicate(nums ,k):
i = 0
for j in range(1,len(nums)):
if nums[i] == nums[j]:
if abs(i-j) <= k:
return True
return False
i += 1
nums = [1,2,3,1,2,3]
k = 2
containsNearbyDuplicate(nums ,k)
What's wrong here ? I am using Sliding-Window approach.