


'arr' = [1, 2, 3]
Here, we have three different types of fruits. We can pick [1, 2] or [2, 3]. We can pick a maximum of two fruits.
Hence, we return 2.
The first line contains an integer ‘n’ representing the number of trees.
The second line has ‘n’ elements of array 'arr' that represent the type of fruit in each tree.
Output is the maximum number of fruits Ninja can put in both baskets after satisfying all the conditions.
You do not need to print anything. It has already been taken care of. Just implement the given function.
We can observe that this problem is the same as finding the maximum subarray that has at most 2 different elements. This problem can be solved as follow-:
This problem follows the Sliding Window pattern. It is quite similar to Longest Subarray with at most two distinct characters (Here fruit types). We can also solve it by maintaining a sliding window as follows.