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.
Be the first one to answer
Add answer anonymously...
Top Infosys System Engineer Specialist interview questions & answers
Top HR questions asked in Infosys System Engineer Specialist
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app