// Cpp program to check if a single bit can
// be flipped to make all ones
#include <bits/stdc++.h>
using namespace std;
// This function returns true if we can
// make all bits the same in the given binary string s.
bool canMakeAllSame(string &s) {
int zeros = 0, ones = 0;
code
// Traverse through the given string and
// count numbers of 0's and 1's
for (char ch : s) {
(ch == '0') ? ++zeros : ++ones;
}
// Return true if any of the two counts is 1
return (zeros == 1 || ones == 1);
The provided C++ program checks whether it's possible to make all characters in a binary string (s) identical by flipping exactly one bit. In other words, it determines if by changing a single '0' to '1' or a single '1' to '0', the entire string can consist of all '0's or all '1's.
Let's break down the program step by step to understand how it achieves this.
Code Breakdown
1. Including Headers and Using Namespace
cpp
#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>: This is a GCC-specific header that includes almost all standard C++ libraries. It's convenient for competitive programming but can slow down compilation for larger projects.
using namespace std;: This allows you to use standard library names without the std:: prefix.
2. Function to Check Flippability
cpp
bool canMakeAllSame(string &s) {
int zeros = 0, ones = 0;
// Traverse through the given string and
// count numbers of 0's and 1's
for (char ch : s) {
(ch == '0') ? ++zeros : ++ones;
}
// Return true if any of the two counts is 1
return (zeros == 1 || ones == 1);
}
Parameters: The function takes a reference to a string s, which is expected to be a binary string (consisting of '0's and '1's).
Counting Zeros and Ones:
It initializes two counters: zeros and ones to keep track of the number of '0's and '1's in the string.
It iterates through each character ch in the string:
If ch is '0', it increments zeros.
If ch is '1', it increments ones.
Determining Flippability:
After counting, if either zerosorones is exactly 1, it means that by flipping this single differing bit, all characters in the string can be made identical.
The function returns true if this condition is met; otherwise, it returns false.
3. Main Function
cpp
int main() {
string s = "101";
canMakeAllSame(s) ? cout << "Yes\n" : cout << "No\n";
return 0;
}
String Initialization: The string s is initialized to "101".
Function Call and Output:
It calls canMakeAllSame(s).
If the function returns true, it prints "Yes".
If the function returns false, it prints "No".
Return Statement: Returns 0 to indicate successful execution.
Example Execution
Let's walk through the provided string "101":
Counting:
'1' → ones = 1
'0' → zeros = 1
'1' → ones = 2
Counts:
zeros = 1
ones = 2
Condition Check:
zeros == 1orones == 1 → 1 == 1or2 == 1 → true
Output: Since the condition is true, the program prints:
code
Yes
This indicates that by flipping the single '0' to '1', the string becomes "111", making all characters identical.
Additional Examples
Let's consider a few more examples to illustrate how the function behaves:
Example 1:
Input: "1110"
Counts: zeros = 1, ones = 3
Output: Yes (Flip the single '0' to '1')
Example 2:
Input: "1001"
Counts: zeros = 2, ones = 2
Output: No (Cannot achieve all '0's or all '1's by flipping just one bit)
Example 3:
Input: "000"
Counts: zeros = 3, ones = 0
Output: No (All bits are already '0'; no need to flip, but per the condition, neither zeros == 1 nor ones == 1 is true)
Example 4:
Input: "1"
Counts: zeros = 0, ones = 1
Output: Yes (By flipping the single '1' to '0', the string becomes "0")
Time and Space Complexity
Time Complexity: The function canMakeAllSame traverses the string once, resulting in a time complexity of O(n), where n is the length of the string.
Space Complexity: The function uses a constant amount of extra space (zeros and ones counters), resulting in O(1) space complexity.
Possible Enhancements
Input Handling:
Currently, the string s is hardcoded. To make the program more flexible, you can modify it to accept user input.
The current function only checks if exactly one bit needs to be flipped. If you want to check if at most one bit needs to be flipped, consider modifying the condition.
cpp
// Allows for zero or one flips
bool canMakeAllSameAtMostOneFlip(const string &s) {
int zeros = 0, ones = 0;
for(char ch : s) {
(ch == '0') ? ++zeros : ++ones;
}
return (zeros <= 1 || ones <= 1);
}
However, in this particular problem, the original condition (zeros == 1 || ones == 1) already encompasses both scenarios where flipping one bit can make the string all '0's or all '1's.
Conclusion
The program effectively determines whether a binary string can be converted to a uniform string (all '0's or all '1's) by flipping a single bit. By counting the number of '0's and '1's, it leverages a simple condition to make this determination with linear time complexity and constant space usage. With some minor enhancements, such as input handling and validation, the program can be made more robust and user-friendly.