Given a binary array, we can flip all the 1 are in the left part and all the 0 to the right part.Calculate the minimum flips required to make all 1s in left and all 0s in right.
Examples :
Input: 1011000 Output: 1 1 flip is required to make it 1111000. Input : 00001 Output : 2 2 flips required to make it 10000.
We have discussed a bitmask based solution in below post. Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
It can be done with O(N) time complexity (where N – number of bits) and O(N) extra memory
- Calculate number of flips of ‘0’ needed to be done while moving from left to right to have all ‘1’ in bits.
- Calculate number of flips of ‘1’ needed to be done while moving from right to left to have all ‘0’ in bits.
- Traversing through all positions between bits and find minimal sum of ‘0’-flips+’1′-flips from both arrays.
C++
// CPP program to find minimum flips required // to make all 1s in left and 0s in right. #include <bits/stdc++.h> using namespace std; int minimalFilps(string bits) { int n = bits.length(); // two arrays will keep the count for number // of 0s' and 1s' to be flipped while // traversing from left to right and right to // left respectively int flipsFromLeft[n]; int flipsFromRight[n]; // Fill flipsFromLeft[] int flips = 0; for (int i = 0; i < n; i++) { if (bits[i] == '0') flips++; flipsFromLeft[i] = flips; } // Fill flipsFromRight[] flips = 0; for (int i = n - 1; i >= 0; i--) { if (bits[i] == '1') flips++; flipsFromRight[i] = flips; } // initialize minFlip to highest int value. If sum // of leftflip and rightFlip is smaller than minflips, // then update minFlips int minFlips = INT_MAX; for (int i = 1; i < n; i++) { if (flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips) minFlips = flipsFromLeft[i - 1] + flipsFromRight[i]; } return minFlips; } // Driver code int main() { string bits = "00001"; cout << minimalFilps(bits) << endl; return 0; } |
Java
// Java program to find minimum flips required // to make all 1s in left and 0s in right. import java.io.*; class GFG { static int minimalFilps(String bits) { int n = bits.length(); // two arrays will keep the count // for number of 0s' and 1s' to be // flipped while traversing from // left to right and right to // left respectively int flipsFromLeft[] = new int[n]; int flipsFromRight[] =new int[n] ; // Fill flipsFromLeft[] int flips = 0; for (int i = 0; i < n; i++) { if (bits.charAt(i) == '0') flips++; flipsFromLeft[i] = flips; } // Fill flipsFromRight[] flips = 0; for (int i = n - 1; i >= 0; i--) { if (bits.charAt(i) == '1') flips++; flipsFromRight[i] = flips; } // initialize minFlip to highest int value. If sum // of leftflip and rightFlip is smaller than minflips, // then update minFlips int minFlips = Integer.MAX_VALUE; for (int i = 1; i < n; i++) { if (flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips) minFlips = flipsFromLeft[i - 1] + flipsFromRight[i]; } return minFlips; } // Driver code public static void main (String[] args) { String bits = "00001"; System.out.println(minimalFilps(bits)); } } // This code is contributed by vt_m. |
Python3
# Python 3 program to find minimum flips required # to make all 1s in left and 0s in right. import sys def minimalFilps(bits): n = len(bits) # two arrays will keep the count for number # of 0s' and 1s' to be flipped while # traversing from left to right and right to # left respectively flipsFromLeft = [0 for i in range(n)] flipsFromRight = [0 for i in range(n)] # Fill flipsFromLeft[] flips = 0 for i in range(0, n, 1): if (bits[i] == '0'): flips = flips + 1 flipsFromLeft[i] = flips # Fill flipsFromRight[] flips = 0 i = n - 1 while(i >= 0): if (bits[i] == '1'): flips = flips + 1 i = i - 1 flipsFromRight[i] = flips # initialize minFlip to highest int value. # If sum of leftflip and rightFlip is smaller # than minflips, then update minFlips minFlips = sys.maxsize for i in range(1, n, 1): if (flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips): minFlips = (flipsFromLeft[i - 1] + flipsFromRight[i]) return minFlips # Driver code if __name__ == '__main__': bits = "00001" print(minimalFilps(bits)) # This code is contributed by # Surendra_Gangwar |
C#
// C# program to find minimum flips required // to make all 1s in left and 0s in right. using System; class GFG { static int minimalFilps(String bits) { int n = bits.Length; // two arrays will keep the count // for number of 0s' and 1s' to be // flipped while traversing from // left to right and right to // left respectively int []flipsFromLeft = new int[n]; int []flipsFromRight =new int[n] ; // Fill flipsFromLeft[] int flips = 0; for (int i = 0; i < n; i++) { if (bits[i] == '0') flips++; flipsFromLeft[i] = flips; } // Fill flipsFromRight[] flips = 0; for (int i = n - 1; i >= 0; i--) { if (bits[i] == '1') flips++; flipsFromRight[i] = flips; } // initialize minFlip to highest int value. // If sum of leftflip and rightFlip is smaller // than minflips, then update minFlips int minFlips = int.MaxValue; for (int i = 1; i < n; i++) { if (flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips) minFlips = flipsFromLeft[i - 1] + flipsFromRight[i]; } return minFlips; } // Driver code public static void Main () { string bits = "00001"; Console.WriteLine(minimalFilps(bits)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find minimum // flips required to make all // 1s in left and 0s in right. function minimalFilps($bits) { $n = strlen($bits); // two arrays will keep the // count for number of 0s' // and 1s' to be flipped // while traversing from // left to right and right // to left respectively $flipsFromLeft[$n] = 0; $flipsFromRight[$n] = 0; // Fill flipsFromLeft[] $flips = 0; for ($i = 0; $i < $n; $i++) { if ($bits[$i] == '0') $flips++; $flipsFromLeft[$i] = $flips; } // Fill flipsFromRight[] $flips = 0; for ($i = $n - 1; $i >= 0; $i--) { if ($bits[$i] == '1') $flips++; $flipsFromRight[$i] = $flips; } // initialize minFlip to // highest int value. If sum // of leftflip and rightFlip // is smaller than minflips, // then update minFlips $INT_MAX=2147483647; $minFlips = $INT_MAX; for ($i = 1; $i < $n; $i++) { if ($flipsFromLeft[$i - 1] + $flipsFromRight[$i] < $minFlips) $minFlips = $flipsFromLeft[$i - 1] + $flipsFromRight[$i]; } return $minFlips; } // Driver Code $bits = "00001"; echo minimalFilps($bits) ; // This code is contributed by nitin mittal. ?> |
Output:
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Count minimum right flips to set all values in an array
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum number of flips with rotation to make binary string alternating
- Min flips of continuous characters to make all characters same in a string
- Number of flips to make binary string alternate | Set 1
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Find Bit whose minimum sequence flips makes all bits same
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Minimum flips required to keep all 1s together in a Binary string
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

