Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String
We will be writing a JavaScript program to find the maximum number of zeros placed consecutively at the start and end of any rotation of a binary string. Our program will take a binary string as input and return the maximum number of zeros that can appear consecutively when considering all possible rotations.
The key insight is that we need to check all rotations of the binary string and count consecutive zeros at both the beginning and end of each rotation, then find the maximum total count.
Approach
To find the maximum number of zeros placed consecutively at the start and end of any rotation of a binary string, follow these steps:
Generate all possible rotations of the binary string.
For each rotation, count consecutive zeros at the start.
For each rotation, count consecutive zeros at the end.
Sum the start and end zeros for each rotation.
Keep track of the maximum sum found across all rotations.
Method 1: Brute Force Approach
This method generates all rotations and counts zeros at start and end of each:
function maxZerosRotation(binaryString) {
let n = binaryString.length;
let maxZeros = 0;
// Check all possible rotations
for (let i = 0; i < n; i++) {
// Create rotation by taking substring from i and appending start
let rotation = binaryString.substring(i) + binaryString.substring(0, i);
// Count zeros at start
let startZeros = 0;
for (let j = 0; j < n && rotation[j] === '0'; j++) {
startZeros++;
}
// Count zeros at end
let endZeros = 0;
for (let j = n - 1; j >= 0 && rotation[j] === '0'; j--) {
endZeros++;
}
// Handle case where entire string is zeros
let totalZeros = (startZeros === n) ? n : startZeros + endZeros;
maxZeros = Math.max(maxZeros, totalZeros);
}
return maxZeros;
}
// Example usage
let binaryString1 = "0110001111";
console.log("Binary string:", binaryString1);
console.log("Maximum zeros:", maxZerosRotation(binaryString1));
let binaryString2 = "000111000";
console.log("Binary string:", binaryString2);
console.log("Maximum zeros:", maxZerosRotation(binaryString2));
Binary string: 0110001111 Maximum zeros: 4 Binary string: 000111000 Maximum zeros: 6
Method 2: Optimized Approach Using Concatenation
This method concatenates the string with itself to efficiently find the maximum consecutive zeros:
function maxZerosOptimized(binaryString) {
let n = binaryString.length;
// Special case: all zeros
if (binaryString === '0'.repeat(n)) {
return n;
}
// Concatenate string with itself
let doubled = binaryString + binaryString;
let maxZeros = 0;
let currentZeros = 0;
// Find maximum consecutive zeros in doubled string
for (let i = 0; i < doubled.length; i++) {
if (doubled[i] === '0') {
currentZeros++;
// Limit to original string length
maxZeros = Math.max(maxZeros, Math.min(currentZeros, n));
} else {
currentZeros = 0;
}
}
return maxZeros;
}
// Test with different examples
let testStrings = ["0110001111", "000111000", "1010101", "0000"];
testStrings.forEach(str => {
console.log(`String: ${str} ? Max zeros: ${maxZerosOptimized(str)}`);
});
String: 0110001111 ? Max zeros: 4 String: 000111000 ? Max zeros: 6 String: 1010101 ? Max zeros: 1 String: 0000 ? Max zeros: 4
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Brute Force | O(n²) | O(n) | High |
| Optimized | O(n) | O(n) | Medium |
How It Works
The brute force method explicitly creates each rotation and counts zeros at both ends. The optimized approach leverages the fact that when we concatenate the string with itself, any rotation's consecutive zeros will appear as a continuous sequence in this doubled string.
For example, with "0110001111":
Original: "0110001111" ? 1 zero at start, 0 at end = 1 total
Rotation "1100011110" ? 0 at start, 1 at end = 1 total
Rotation "1000111100" ? 0 at start, 2 at end = 2 total
Rotation "0001111001" ? 3 at start, 0 at end = 3 total
Best rotation gives us 4 consecutive zeros
Conclusion
Both approaches solve the problem effectively. The brute force method is easier to understand, while the optimized approach using string concatenation provides better time complexity for larger inputs.
