Given an integer array, print all maximum size subarrays having all distinct elements in them.

For example,

Input:  A[] = { 5, 2, 3, 5, 4, 3 }
 
Output: The largest subarrays with all distinct elements are:
 
{ 5, 2, 3 }
{ 2, 3, 5, 4 }
{ 5, 4, 3 }

Practice this problem

The problem differs from the problem of finding the maximum size subsequence with distinct elements. Unlike subsequences, subarrays are required to occupy consecutive positions within the original array.

 
We can use a sliding window to solve this problem easily. The idea is to maintain a window with an invariant that all elements inside it must be distinct. The solution keeps on expanding the window to the right, and if any duplicate is encountered, it shrinks the window from the left until all elements are distinct again. To keep track of distinct elements inside a window, use a map.

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

C++


Download  Run Code

Output:

5, 2, 3
2, 3, 5, 4
5, 4, 3

Java


Download  Run Code

Output:

5, 2, 3
2, 3, 5, 4
5, 4, 3

Python


Download  Run Code

Output:

[5, 2, 3]
[2, 3, 5, 4]
[5, 4, 3]

The time complexity of the above solution is O(n), where n is the input size and requires O(n) extra space to mark elements in the current window.