
- 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
Binary representation of next greater number with same number of 1’s and 0’s in C Program?
Suppose we have one binary number, that is representation of a number n. We have to find binary representation of a number which is smallest but larger than n, and it also has same number of 0s and 1s. So if the number is 1011 (11 in decimal), then the output will be 1101 (13). This problem can be found using the next-permutation calculation. Let us see the algorithm to get the idea.
Algorithm
nextBin(bin) −
Begin len := length of the bin for i in range len-2, down to 1, do if bin[i] is 0 and bin[i+1] = 1, then exchange the bin[i] and bin[i+1] break end if done if i = 0, then there is no change, return otherwise j:= i + 2, k := len – 1 while j < k, do if bin[j] is 1 and bin[k] is 0, then exchange bin[j] and bin[k] increase j and k by 1 else if bin[i] is 0, then break else increase j by 1 end if done return bin End
Example
#include <iostream> using namespace std; string nextBinary(string bin) { int len = bin.size(); int i; for (int i=len-2; i>=1; i--) { if (bin[i] == '0' && bin[i+1] == '1') { char ch = bin[i]; bin[i] = bin[i+1]; bin[i+1] = ch; break; } } if (i == 0) "No greater number is present"; int j = i+2, k = len-1; while (j < k) { if (bin[j] == '1' && bin[k] == '0') { char ch = bin[j]; bin[j] = bin[k]; bin[k] = ch; j++; k--; } else if (bin[i] == '0') break; else j++; } return bin; } int main() { string bin = "1011"; cout << "Binary value of next greater number = " << nextBinary(bin); }
Output
Binary value of next greater number = 1101
- Related Articles
- Largest number with binary representation is m 1’s and m-1 0’s in C++
- Binary representation of next number in C++
- Count subarrays with equal number of 1’s and 0’s in C++
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Maximum 0’s between two immediate 1’s in binary representation in C++
- Count the number of 1’s and 0’s in a binary array using STL in C++
- 1’s and 2’s complement of a Binary Number?
- Find next greater number with same set of digits in C++
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- Find longest sequence of 1’s in binary representation with one flip in C++
- Design DFA for language over {0,1} accepting strings with odd number of 1’s and even number of 0’s
- Previous number same as 1’s complement in C++
- Count number of binary strings without consecutive 1's in C
- Next greater Number than N with the same quantity of digits A and B in C++

Advertisements