Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6380 |

@mb1973 | 5704 |

@Quandray | 5245 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5046 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3523 |

@mantu_singh | 3510 |

@sushant_a | 3459 |

Complete Leaderboard | |

Handle | Score |

@cfwong8 | 1156 |

@gurshehzadsingh | 676 |

@nithinreddy3210 | 642 |

@jha8768 | 577 |

@Run_for_life | 574 |

@divyagupta221100 | 557 |

@AkashLahoty | 537 |

@aayushdesai01 | 526 |

@riyakumaribkj | 526 |

@2017akku | 521 |

@amansinha74458 | 503 |

Complete Leaderboard |

Given an array **A[]** consisting of **0’s** and **1’s**. A flip operation is one in which you turn **1** into **0** and a **0** into **1**. You have to do at most one “Flip” operation of any subarray. Formally, select a range **(l, r) **in the array **A[]**, such that (0 ≤ l ≤ r < n) holds and flip the elements in this range to get the maximum ones in the final array. You can possibly make zero operations to get the answer.

**Example 1:**

Input:N = 5 A[] = {1, 0, 0, 1, 0}Output:4Explanation:We can perform a flip operation in the range [1,2] After flip operation array is : [11 11 0] Count of one after fliping is : 4[Note: the subarray marked in bold is the flipped subarray]

**Example 2:**

Input:N = 7 A[] = {1, 0, 0, 1, 0, 0, 1}Output:6Explanation:We can perform a flip operation in the range [1,5] After flip operation array is : [11 1 0 1 11] Count of one after fliping is : 6[Note: the subarray marked in bold is the flipped subarray]

**Your Task: **

You don't need to read input or print anything. Your task is to complete the function **maxOnes()** which takes the array **A[]** and its size **N**** **as inputs and returns the maximum number of 1's you can have in the array after atmost one flip operation.

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 ≤ N ≤ 10^{4}

0 ≤ A[i] ≤ 1

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Flip Bits

...