Given a binary input that represents binary representation of positive number n, find binary representation of smallest number greater than n with same number of 1’s and 0’s as in binary representation of n. If no such number can be formed, print “no greater number”.
The binary input may be and may not fit even in unsigned long long int.
Input : 10010 Output : 10100 Here n = (18)10 = (10010)2 next greater = (20)10 = (10100)2 Binary representation of 20 contains same number of 1's and 0's as in 18. Input : 111000011100111110 Output : 111000011101001111
This problem simply boils down to finding next permutation of a given string. We can find the next_permutation() of the input binary number.
Below is an algorithm to find next permutation in binary string.
- Traverse the binary string bstr from the right.
- While traversing find the first index i such that bstr[i] = ‘0’ and bstr[i+1] = ‘1’.
- Exchange character of at index ‘i’ and ‘i+1’.
- Since we need smallest next value, consider substring from index i+2 to end and move all 1’s in the substring in the end.
Below is the implementation of above steps.
Binary representation of next greater number = 10100
Time Complexity : O(n) where n is number of bits in input.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Binary representation of next number
- Binary representation of previous number
- Occurrences of a pattern in binary representation of a number
- Check if actual binary representation of a number is palindrome
- Find the number obtained after concatenation of binary representation of M and N
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Minimum number of distinct powers of 2 required to express a given binary number
- Number of ways to split a binary number such that every part is divisible by 2
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Minimum number of Binary strings to represent a Number
- Number of steps required to convert a binary number to one
- Maximum number of splits of a binary number
- Smallest number greater than X which is K-periodic
- Count number of substrings with numeric value greater than X
- Smallest number greater than Y with sum of digits equal to X
- Count the number of words having sum of ASCII values less than and greater than k
- Decimal representation of given binary string is divisible by 5 or not
- Longest common substring in binary representation of two numbers
- Construct Binary Tree from String with bracket representation
- Permutation of a string with maximum number of characters greater than its adjacent characters