## kesar

BAN USER- 0of 0 votes

AnswersYou are given a binary array with N elements: d[0], d[1], ... d[N - 1].

- kesar in United States

You can perform AT MOST one move on the array: choose any two integers [L, R], and flip all the elements between (and including) the L-th and R-th bits. L and R represent the left-most and right-most index of the bits marking the boundaries of the segment which you have decided to flip.

What is the maximum number of '1'-bits (indicated by S) which you can obtain in the final bit-string?

'Flipping' a bit means, that a 0 is transformed to a 1 and a 1 is transformed to a 0 (0->1,1->0).

Input Format

An integer N

Next line contains the N bits, separated by spaces: d[0] d[1] ... d[N - 1]

Output:

S

Constraints:

1 <= N <= 100000

d[i] can only be 0 or 1f

0 <= L <= R < n

Sample Input:

8

1 0 0 1 0 0 1 0

Sample Output:

6

Explanation:

We can get a maximum of 6 ones in the given binary array by performing either of the following operations:

Flip [1, 5] ==> 1 1 1 0 1 1 1 0| Report Duplicate | Flag | PURGE

VMWare Inc Problem Solving

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

If array sorted descending order like 6,5,4,3,2,1 rotated like 3 2 1 6 5 4 then it fails.

- kesar February 02, 2014