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
4mo

Given an array of 0s and 1s, find the maximum count of 1s by flipping a sub-array at most once.

  • Iterate through the array and keep track of the maximum count of 1s obtained by flipping a sub-array.

  • Cons...read more

Help your peers!
Select
Add answer anonymously...

One Convergence Software Developer interview questions & answers

A Software Developer was asked Q. Can you explain the memory layout of a C program?
A Software Developer was asked Q. What is the difference between Zombie, Orphan, and Daemon processes?
A Software Developer was asked Q. What is a symbol table?

Popular interview questions of Software Developer

A Software Developer was asked Q1. Can you explain the memory layout of a C program?
A Software Developer was asked Q2. What is the difference between Zombie, Orphan, and Daemon processes?
A Software Developer was asked Q3. What is a symbol table?
One Convergence 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