Problem statement:

Difficulty: Medium

Given an array of characters where each character represents a fruit tree, you are given two baskets and your goal is to put maximum number of fruits in each basket. The only restriction is that each basket can have only one type of fruit.

You can start with any tree, but once you have started you can’t skip a tree. You will pick one fruit from each tree until you cannot, i.e., you will stop when you have to pick from a third fruit type.

Write a function to return the maximum number of fruits in both the baskets.

Example:

Input: Fruit=['A', 'B', 'C', 'A', 'C']
Output: 3
Explanation: We can put 2 'C' in one basket and one 'A' in the other from the subarray 
['C', 'A', 'C']

Example 2:

Input: Fruit=['A', 'B', 'C', 'B', 'B', 'C']
Output: 5
Explanation: We can put 3 'B' in one basket and two 'C' 
in the other basket. This can be done if we start with the second letter: 
['B', 'C', 'B', 'B', 'C']

Solution

class MaxFruitCountOf2Types {
  public static int findLength(char[] arr) {
    if (arr.length == 0) return 0;
    Map<Character, Integer> map = new HashMap<>();
        int start = 0, end = 0, len = 0;
        while (end < arr.length) {
            map.put(arr[end], map.getOrDefault(arr[end], 0) + 1);
            end++;
            while (map.size() > 2) {
                map.put(arr[start], map.getOrDefault(arr[start], 0) - 1);
                if (map.get(arr[start]) == 0) {
                    map.remove(arr[start]);
                }
                start++;
            }
            len = Math.max(len, end - start);
        }
    return len;    
  }
}

Time complexity:

O(n)

Space complexity:

O(1) coz k is fixed = 2

Approach: