Flip Bits Problem Explanation

Given an array of integers ARR of size N, consisting of 0s and 1s, you need to select a sub-array and flip its bits. Your task is to return the maximum count of 1s that can be obtained by flipping a chosen sub-array at most once.

Input:

The input consists of a single integer T (number of test cases). For each test case:

  • An integer N, representing the size of the array.
  • An array of N space-separated integers representing the elements of the array.

Output:

For each test case, output a single integer representing the maximum number of 1s you can have in the array after performing at most one flip operation.

Example:

Input:
T = 1
N = 5
ARR = [1, 1, 0, 0, 1]
Output:
5
Explanation:

By flipping the sub-array from index 2 to 3, the resulting array becomes [1, 1, 1, 1, 1], which contains five 1s. Hence, the output is 5.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 10^4
  • 0 <= ARR[i] <= 1
Note:

You are not required to print anything. Implement the given function to return the answer.

AnswerBot
2mo

Maximize the count of 1s in a binary array by flipping a sub-array of 0s to 1s at most once.

  • Initial Count: Start by counting the number of 1s in the original array.

  • Flipping Logic: For each sub-array, ...read more

Help your peers!
Select
Add answer anonymously...
EX Squared Solutions Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits