Flipping Coins Problem
Gary has N coins placed in a straight line. Some coins have their head side up, and others have the tail side up.
Convention:
1 denotes the HEAD side is up.
0 denotes the TAIL side is up.
Gary wants to achieve the maximum number of head-side up coins. He is allowed to perform at most one flip (possibly zero) in which he can flip the coins of a continuous segment (a continuous subarray).
Example:
In the given array (0-based indexing), { 1, 0, 0, 1, 0, 0, 1 }
, the maximum number of head-side up coins can be achieved by flipping the coins in the range 1 to 5. The array will then become { 1, 1, 1, 0, 1, 1, 1 }
.
Task:
Return the maximum number of heads side up Gary can obtain.
Input:
The first line contains an integer 'T' which denotes the number of test cases.
For each test case:
- The first line contains an integer 'N' denoting the number of coins.
- The second line contains 'N' space-separated integers (0 or 1) representing the orientation of the coins.
Output:
For each test case, print the maximum number of head-side up coins obtainable in a single line.
Output for each test case is printed on a separate line.
Constraints:
1 <= T <= 10
1 <= N <= 10^5
0 <= arr[i] <= 1
Be the first one to answer
Add answer anonymously...
Top Athenahealth Technology Software Developer interview questions & answers
Popular interview questions of Software Developer
>
Athenahealth Technology Software Developer Interview Questions
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