
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print n 0s and m 1s such that no two 0s and no three 1s are together in C Program
There should be sequence of N 0’s and M 1’s such that the sequence so formed shouldn’t contain two consecutive 0’s with three consecutive 1’s.
Input − N=5 M=9
Output − 1 1 0 1 1 0 1 1 0 1 0 1 0 1
Note − To make the above sequence, the statement (m < n-1) || m >= 2 * (n + 1) should be false if it is true than we can’t make the above sequence.
It is advisable to first go through question logic and try yourself instead of jumping to the solution directly given below.
Algorithm
START Step 1 -> take values in ‘n’ and ‘m’ Step 2 -> Loop IF m=n-1 Loop While m>0 and n>0 Print 01 Decrement m and n by 1 End Loop While Loop IF n!=0 Print 0 End IF Loop IF m!=0 Print 1 End IF Step 3-> Else (m < n-1) || m >= 2 * (n + 1) Print cn’t have sequence for this Step 4 -> Else Loop While m-n > 1 && n > 0 Print 1 1 0 Decrement m by 2 and n by 1 End While Loop While n>0 Print 1 0 Decrement m and n by 1 End While Loop While m>0 Print 1 Decrement m by 1 End While Step 5-> End Else STOP
Example
#include <stdio.h> #include <math.h> int main() { int n =5, m=9; if( m == n-1 ) { //If m is 1 greater than n then consecutive 0's and 1's while( m > 0 && n > 0 ) { //Loop until all m's and n's printf("01"); m--; n--; } if ( n!=0 ) //Print the remaining 0 printf("0"); if( m!=0 ) //Print the remaining 1 printf("1"); } else if ( (m < n-1) || m >= 2 * (n + 1) ) { //If this is true the sequence can't be made printf("Can't have sequence for this
"); } 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--; } } return 0; }
Output
If we run above program then it will generate following output.
1 1 0 1 1 0 1 1 0 1 0 1 0 1
- Related Articles
- Python - List Initialization with alternate 0s and 1s
- C Program to construct DFA accepting odd numbers of 0s and 1s
- XOR counts of 0s and 1s in binary representation in C++
- Largest subarray with equal number of 0s and 1s in C++
- Count Substrings with equal number of 0s, 1s and 2s in C++
- JavaScript Program for Sorting a Linked List of 0s, 1s, And 2s
- JavaScript Program for Counting sets of 1s and 0s in a binary matrix
- Check if a string has m consecutive 1s or 0s in Python
- Segregate all 0s on right and 1s on left in JavaScript
- Count all 0s which are blocked by 1s in binary matrix in C++
- Minimum flips to make all 1s in left and 0s in right in C++
- Encoding a number string into a string of 0s and 1s in JavaScript
- Maximum count of 0s between two 1s in the given range for Q queries
- Constructing a string of alternating 1s and 0s of desired length using JavaScript
- Maximum subsequence sum such that no three are consecutive in C++ Program

Advertisements