You are given a binary array bits (every element is 0 or 1).
You may choose at most one contiguous subarray and invert every element in it, meaning each 0 becomes 1 and each 1 becomes 0. You may also choose to invert nothing.
Return the maximum possible number of 1s in the array after the operation.
Examples
Example 1
Input: bits = [1, 0, 0, 1, 0, 1]
Output: 5
Explanation: Invert the subarray from index 1 to index 4: [1, 1, 1, 0, 1, 1].
The array now contains five 1s. No choice does better.
Example 2
Input: bits = [1, 1, 1]
Output: 3
Explanation: Every element is already 1, so inverting any subarray would only
lose 1s. Invert nothing.
Constraints
1≤bits.length≤105
bits[i] is 0 or 1.
Loading editor...
Run checks the sample cases; Submit runs every case.
Samples 2
Custom 0
passedwrong answertime limiterrorran, no expected valuenot run