Smallest Subarray With K Distinct Elements

Easy
0/40
Average time to solve is 20m
profile
Contributed by
96 upvotes
Asked in companies
IntuitUberGoldman Sachs

Problem statement

Given an array 'A' consisting of 'N' integers, find the smallest subarray of 'A' containing exactly 'K' distinct integers.

Note :
If more than one such contiguous subarrays exist, consider the subarray having the smallest leftmost index.

For example - if A is [1, 2, 2, 3, 1, 3 ] and k = 2 then the subarrays: [1,2], [2,3], [3,1], [1,3] are the smallest subarrays containing 2 distinct elements. In this case, we will consider the starting and ending index of subarray [1,2] i.e. 0 and 1.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains two integers 'N' and 'K' denoting the total number of integers and number of distinct integers respectively. 

The second line contains 'N' space-separated integers describing elements of the array 'A'.
Output Format :
Print two space-separated integers denoting the starting and ending index of the subarray if it exists, otherwise print -1.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Assume array starts with 0 index.
If more than one solution is possible then print the subarray with smaller left index.
Constraints :
1 <= N, K <= 10^6
-10^5 <= A[i] <= 10^5

Time limit: 1 sec
Sample Input 1 :
4 3
1 1 2 1 2
Sample Output 1 :
-1
Explanation Of Sample Input 1 :
The value of k = 3 and there are only two distinct elements in the given array i.e 2 and 3. Therefore there exist no subarray with 3 distinct elements.
Sample Input 2 :
8 3
4 2 2 2 3 4 4 3 
Sample Output 2 :
3 5
Hint

Explore all subarrays and find the smallest one containing ‘k’ distinct elements.

Approaches (2)
Brute Force Approach

Algorithm

 

  • Pick each element from the array as the starting element(i) of the subarray,
    • Initialize an empty set to store distinct elements in the subarray.
    • Pick each remaining element(i, i+1,..n - 1) from the array as the last element(jth).
      • Add the current element to the set.
      • If the set size equals k then update the results and break from the inner loop(we have already found k distinct elements increasing the size of the subarray will either make more distinct elements or increase the subarray size with repeated elements which are not to be considered in the required results).
    • If j == size of the array, it means we have not found any desired subarray starting from ith index and going forward we will be having fewer elements to consider. Break from the outer loop
  • Print the output if found, otherwise, print “-1”.
Time Complexity

O(N^2), where ‘N’ is the number of elements in the array.

 

Picking the ends of the subarray using two nested loops(one inside another) makes the time complexity O(N^2).

Space Complexity

O(N), where ‘N’ is the number of elements in the array.

 

In the worst case, we can have all ‘N’ elements in our set.

Code Solution
(100% EXP penalty)
Smallest Subarray With K Distinct Elements
Full screen
Console