Max Continuous Series of 1s

You are given with an array of 1s and 0s. And you are given with an integer M, which signifies number of flips allowed.
Find the position of zeros which when flipped will produce maximum continuous series of 1s.

For this problem, return the indices of maximum continuous series of 1s in order.

Example:

Input : 
Array = {1 1 0 1 1 0 0 1 1 1 } 
M = 1

Output : 
[0, 1, 2, 3, 4] 

If there are multiple possible solutions, return the sequence which has the minimum start index.

Method1:

Start from last index, every time we see a 0, if there is flip available, treat it as zero.

Solution:

Time: O(n^2)
Space: O(1)

public class Solution {
    public ArrayList<Integer> maxone(ArrayList<Integer> A, int B) {
        int right = A.size() - 1;
        int maxRight = A.size() - 1;
        int maxLeft = A.size() - 1;
        int oldB = B;
        while (right > 0) {
            int left = right - 1;
            int rightVal = A.get(right);
            if (rightVal == 1 || B > 0) {
                if (rightVal == 0) { 
                    B --;
                }
                while (left >= 0) {
                    int leftVal = A.get(left);
                    if (leftVal == 1 || B > 0) {
                        if (leftVal == 0) {
                            B --;
                        }
                        if (right - left >= maxRight - maxLeft) {
                            maxRight = right;
                            maxLeft = left;
                        }
                    } else {
                        B = oldB;
                        break;
                    }
                    left --;
                }
            } else {
                B = oldB;
            }
            right --;
        }
        ArrayList<Integer> result = new ArrayList<>();
        for (int i = maxLeft; i <= maxRight; i ++) {
            result.add(i);
        }
        return result;
    }
}

Method2:

Sliding window. We keep a window where number of zeros is less than or equal to B. If less than or equal to B, we increase window by increasing right, otherwise we decrease window by increasing left.

Time: O(n)
Space: O(1)

public class Solution {
    public ArrayList<Integer> maxone(ArrayList<Integer> A, int B) {
        if (A == null || A.size() == 0) return new ArrayList<Integer>();
        int left = 0;
        int right = 1;
        int max = 0;
        int maxLeft = 0;
        int maxRight = 0;
        int currZero = A.get(0) == 0 ? 1 : 0;
        while (right < A.size()) {
            while (right < A.size() && currZero <= B) {
                int rightVal = A.get(right);
                if (rightVal == 0) {
                    currZero ++;
                }
                if (currZero <= B && right - left > maxRight - maxLeft) {
                    maxRight = right;
                    maxLeft = left;
                }
                right ++;
            }
            while (left < A.size() && currZero > B) {
                int leftVal = A.get(left);
                if (leftVal == 0) {
                    currZero --;
                }
                left ++;
            }
        }
        ArrayList<Integer> result = new ArrayList<>();
        for (int i = maxLeft; i <= maxRight; i ++) {
            result.add(i);
        }
        return result;
    }
}