Smallest Subarray With K Distinct Numbers in JavaAn array containing integers is given to us. Also, an integer k is given. Our task is to find the array comprising of the minimum range [lft, rght], (both lft and rght are inclusive in the range), such that exactly k different numbers are there in the range. If it is not possible to find the k different elements in the given input array, then a suitable message should be shown on the console. Observe the following examples. Example 1: Input arr[] = { 11, 11, 11, 12, 12, 13, 13, 14, 15, 17 }, k = 4 Output: {6, 9} Explanation: The range [6, 9] consists of four distinct elements. Also, this window size or the range size is the smallest. Example 2: Input arr[] = { 11, 12, 12, 13}, k = 3 Output: {0, 3} Explanation: The range [0, 3] contains three distinct elements. Also, this window size or the range size is the smallest. Example 3: Input arr[] = { 11, 12, 12, 12, 13, 13}, k = 5 Output: It is not possible to find the five distinct elements in the input array arr[]. Explanation: The input array only contains three distinct elements. Hence, finding five distinct elements is not possible. Naïve Approach:The naive approach of the problem is to find out all of the subarrays and then check whether the subarray is of the size k or not. However, there are some points that one needs to take into consideration. Algorithm:Step 1: Pick each element of the provided input array as the beginning element [j-th element] of the required subarray. Step 2: In each iteration, create an empty set for keeping the unique sub-array elements.
Step 3: If (k == n), then it means any required subarray beginning from the jth index is not found. Therefore, the outer loop should be terminated. Step 4: Display the found output if found. Else, an appropriate message should be printed on the console. ImplementationLet's do the implementation of the above steps. Observe the following program. FileName: SmallestSubarray.java Output: For the input array: 11 11 11 12 12 13 13 14 15 17 The smallest subarray containing 4 distinct elements ranges from: [6, 9] For the input array: 11 12 12 13 The smallest subarray containing 3 distinct elements ranges from: [0, 3] For the input array: 11 12 12 12 13 13 The smallest subarray containing 5 distinct elements is not found. Complexity Analysis: The program uses the nested for-loop making the time complexity of the program O(n2). Also, the program uses the hash set, making the space complexity of the program O(n), where n is the total number of elements present in the input array. Now, we will do some optimization to reduce the time and space complexity. Approach: Sliding WindowUsing the sliding window, we can reduce the time complexity, as well as, the space complexity of the program. But before writing the program, let's see the steps involved to achieve the result. Step 1: Create a map for keeping the number of occurrences of each element. Step 2: Take two variables: st and ed, of the required subarray. Step 3: Here, we will use j and k as the beginning point and the ending point of the window, respectively, and assign j = 0 and k = 0. Step 4: Traverse the input array till the end pointer (k) of the window reaches the end of the input array. i.e., while( k < n) do the following:
ImplementationLet's see the implementation of the above steps. FileName: SmallestSubarray1.java Output: For the input array: 11 11 11 12 12 13 13 14 15 17 The smallest subarray containing 4 distinct elements ranges from: [6, 9] For the input array: 11 12 12 13 The smallest subarray containing 3 distinct elements ranges from: [0, 3] For the input array: 11 12 12 12 13 13 The smallest subarray containing 5 distinct elements is not found. Complexity Analysis: In the worst-case scenario, an element can be added or removed from the hash map only once. Thus, the time complexity of the program is O(n), where n is the total number of elements present in the input array. In the worst case, the hash map can only have k elements. Thus, the space complexity of the program is O(k). Next TopicString Sort Custom in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India