Given an array and a positive number k, check whether the array contains any duplicate elements within the range k. If k is more than the array’s size, the solution should check for duplicates in the complete array.

For example,

Input:
 
nums[] = { 5, 6, 8, 2, 4, 6, 9 }
k = 4
 
Output: Duplicates found
 
(element 6 repeats at distance 4 which is <= k)
 
 
Input:
 
nums[] = { 5, 6, 8, 2, 4, 6, 9 }
k = 2
 
Output: No duplicates were found
 
(element 6 repeats at distance 4 which is > k)
 
 
Input:
 
nums[] = { 1, 2, 3, 2, 1 }
k = 7
 
Output: Duplicates found
 
(element 1 and 2 repeats at distance 4 and 2, respectively which are both <= k)

Practice this problem

A naive solution would be to consider every subarray of size k and check for duplicates in it. The time complexity of this solution is O(n.k2) since there can be n subarrays of size k, and each subarray might take O(k2) time for checking duplicates.

 
The problem can be efficiently solved using hashing in O(n) time and O(n) extra space. The idea is to traverse the array and store each element and its index in a map, i.e., (element, index) as (key, value) pairs in a map. If any element is already found present on the map, check if that element repeats within the range of k using its previous occurrence information from the map.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

Duplicates found

Java


Download  Run Code

Output:

Duplicates found

Python


Download  Run Code

Output:

Duplicates found

We can also use a sliding window to solve the above problem. The idea is to process every window of size k and store its elements in a set. If any element repeats in the window, we can say that it repeats within the range of k.

Initially, our window will contain the first k elements of the input. Then for each item of the remaining input, add it to the current window. While adding the i'th item of the input to the current window, remove the (i-k)'th element from it. This ensures the efficiency of the solution and keeps the window balance at any point.

 
Following is the C++, Java, and Python implementation based on the above idea:

C++


Download  Run Code

Output:

Duplicates found

Java


Download  Run Code

Output:

Duplicates found

Python


Download  Run Code

Output:

Duplicates found

The time complexity of the above solution is O(n) and requires O(k) extra space.