
Consider ‘fruits’ = [1, 2, 1, 3, 2], the longest subarray following the given condition is [1, 2, 1]. The length of the sub-array is 3.
The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the size of the array ‘fruits’.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the array ‘fruits’.
For each test case, print a single integer representing the length of the largest sub-array, which can be formed such that the count of distinct numbers in it is less than or equal to 2.
The output of each test case will be printed in a separate line.
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
1 < =T <= 10
1 <= N <= 10 ^ 6
1 <= fruits[i] <= 10 ^ 9
Time limit: 1 sec
We will try every possible combination and check for the count of unique elements. Whenever the unique elements are greater than 2, we will change the sub-array.
Algorithm:
We will create a sliding window that will keep track of the distinct elements. Initially, we will increase the size of our window and try to add as many elements as possible. After that, when the total number of distinct elements in the array is more than 2, then we will try to reduce the size of our window. We will keep updating the answer. Finally, we will return the overall maximum answer.
Algorithm: