Created
August 2, 2015 03:01
-
-
Save ravichandrae/123229f5b8e1ee157ebd to your computer and use it in GitHub Desktop.
Given an array of numbers A, which might contain duplicate elements, How do we find if there are two elements A[i], A[j] such that i, j are distinct and the difference between them is at most k?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean containsNearbyDuplicate(int[] nums, int k) | |
{ | |
Map<Integer, Integer> map = new HashMap<Integer,Integer>(); | |
for(int i = 0; i < nums.length; i++ ) | |
{ | |
if(map.containsKey(nums[i]) && i-map.get(nums[i]) <= k) | |
return true; | |
else | |
map.put(nums[i],i); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment