Contains Duplicate II Visualizer & Solution | Rulcode
Loading problem details...
222. Contains Duplicate II
PRO
Easy
Topics
Companies
Hint
Understand Before Coding
Loading visualization...
Visualize This Problem
Open Visualizer
Draw Your Approach
Draw
Example 1:
Input:nums = [1,2,3,1], k = 3
Output:true
Explanation:The number 1 appears at index 0 and 3. The absolute difference of their indices is <code>abs(0 - 3) = 3</code>, which is less than or equal to <code>k = 3</code>.
Example 2:
Input:nums = [1,0,1,1], k = 1
Output:true
Explanation:The number 1 at index 2 and the number 1 at index 3 have an absolute difference of indices, <code>abs(2 - 3) = 1</code>, which is less than or equal to <code>k = 1</code>.
Example 3:
Input:nums = [1,2,3,1,2,3], k = 2
Output:false
Explanation:No two duplicate numbers have an absolute index difference less than or equal to 2.
Constraints:
•
•
•
Time Complexity
O(N)
Space Complexity
O(min(N, k+1))
Step 1 / 27
Speed: 1.0x
Contains Duplicate II (Sliding Window Set)
Input Array & Window
right
1
left
2
3
1
1
Window Set Content
Empty Set
Commentary
Check if the array contains any duplicate values within a distance of k = 2. We will maintain a sliding window of size at most k + 1.