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
Selected Reading
Print n 0s and m 1s such that no two 0s and no three 1s are together in C Program
In C programming, we need to print a sequence of N zeros and M ones such that no two zeros are consecutive and no three ones are consecutive. This is a constraint satisfaction problem that requires careful arrangement of digits.
Syntax
// Condition to check if sequence is possible
if ((m < n-1) || m >= 2 * (n + 1))
// Sequence not possible
else
// Generate valid sequence
Algorithm
The algorithm works in three main cases −
- Case 1: When m = n-1, alternate between 0 and 1
- Case 2: When sequence is impossible: (m < n-1) || m >= 2 * (n + 1)
- Case 3: General case: place two 1s followed by one 0, then alternate
Example: Valid Sequence Generation
Here's a complete program to generate the required sequence −
#include <stdio.h>
int main() {
int n = 5, m = 9; // n zeros, m ones
printf("Input: N=%d, M=%d<br>", n, m);
printf("Output: ");
if (m == n - 1) {
// Case 1: m is exactly n-1
while (m > 0 && n > 0) {
printf("0 1 ");
m--;
n--;
}
if (n != 0) printf("0 ");
if (m != 0) printf("1 ");
}
else if ((m < n - 1) || m >= 2 * (n + 1)) {
// Case 2: Impossible sequence
printf("Can't generate valid sequence");
}
else {
// Case 3: General case
// First place groups of "1 1 0" while possible
while (m - n > 1 && n > 0) {
printf("1 1 0 ");
m -= 2;
n--;
}
// Then alternate "1 0" for remaining pairs
while (n > 0) {
printf("1 0 ");
n--;
m--;
}
// Print remaining 1s
while (m > 0) {
printf("1 ");
m--;
}
}
printf("<br>");
return 0;
}
Input: N=5, M=9 Output: 1 1 0 1 1 0 1 1 0 1 0 1 0 1
Example: Testing Different Cases
Let's test various input combinations to understand the constraints −
#include <stdio.h>
void generateSequence(int n, int m) {
printf("N=%d, M=%d: ", n, m);
if (m == n - 1) {
while (m > 0 && n > 0) {
printf("0 1 ");
m--; n--;
}
if (n != 0) printf("0 ");
if (m != 0) printf("1 ");
}
else if ((m < n - 1) || m >= 2 * (n + 1)) {
printf("Invalid sequence");
}
else {
while (m - n > 1 && n > 0) {
printf("1 1 0 ");
m -= 2; n--;
}
while (n > 0) {
printf("1 0 ");
n--; m--;
}
while (m > 0) {
printf("1 ");
m--;
}
}
printf("<br>");
}
int main() {
generateSequence(5, 9); // Valid case
generateSequence(3, 2); // Invalid: m < n-1
generateSequence(2, 8); // Invalid: m >= 2*(n+1)
generateSequence(4, 5); // Valid case
return 0;
}
N=5, M=9: 1 1 0 1 1 0 1 1 0 1 0 1 0 1 N=3, M=2: Invalid sequence N=2, M=8: Invalid sequence N=4, M=5: 1 0 1 0 1 0 1 0 1
Key Points
- The constraint (m < n-1) || m >= 2 * (n + 1) determines if a valid sequence exists
- When m = n-1, we can simply alternate starting with 0
- For general cases, we use "1 1 0" pattern first, then "1 0" pattern
- This ensures no two 0s are consecutive and no three 1s are consecutive
Conclusion
This algorithm efficiently generates sequences with given constraints by using a three-case approach. The key is understanding when sequences are possible and applying the appropriate pattern generation strategy.
Advertisements
