Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Flipping Coins
MEDIUM
32 mins
8 upvotes
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Flipping Coins

Contributed by
Ashwani
Medium
yellow-spark
0/80
Avg time to solve 32 mins
Success Rate 68 %
Share
8 upvotes

Problem Statement

Gary has N coins placed in a straight line. Some coins have 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. 

Now, Gary wants to obtain a maximum number of head-side up coins. He can perform at most one(possibly 0) flip in which he can flip the coins of a continuous interval (continuous subarray).

For example: In the given array (0 based indexing), { 1, 0, 0, 1, 0, 0, 1 }, we can obtain maximum head side up coins by flipping the coins in range 1 to 5. The array will thus become {1, 1, 1, 0, 1, 1, 1 }.

Return the maximum number of heads side up Gary can obtain.

Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample input 1:
1
4
1 0 1 0 
Sample output 1:
3
Explanation
{ 1, 1, 1, 0} is one possible answer if we flip range 1-1. 
Sample input 2:
2
5
0 0 1 0 1
3
1 1 1
Sample output 2:
4
3
Reset Code
Full screen
Auto
copy-code
Console