Binary representation of next number
Given a binary input that represents binary representation of positive number n, find a binary representation of n+1.
The binary input may be and may not fit even in unsigned long long int.
Examples:
Input: 10011
Output: 10100
Explanation:Here n = (19)10 = (10011)2
next greater integer = (20)10 = (10100)2Input: 111011101001111111
Output: 111011101010000000
Approach 1:
We store input as a string so that large numbers can be handled. We traverse the string from the rightmost character and convert all 1s to 0s until we find a 0. Finally, convert the found 0 to 1. If we do not find a 0, we append a 1 to the overall string.
string nextGreater(num)
l = num.length
// Find first 0 from right side. While
// searching, convert 1s to 0s
for i = l-1 to 0
if num[i] == '0'
num[i] = '1'
break
else
num[i] = '0'
// If there was no 0
if i < 0
num = '1' + num
return num
Below is the implementation of the above idea.
C++
// C++ implementation to find the binary // representation of next greater integer #include <bits/stdc++.h> using namespace std; // function to find the required // binary representation string nextGreater(string num) { int l = num.size(); int i; // examine bits from the right for (i = l - 1; i >= 0; i--) { // if '0' is encountered, convert // it to '1' and then break if (num.at(i) == '0' ) { num.at(i) = '1' ; break ; } // else convert '1' to '0' else num.at(i) = '0' ; } // if the binary representation // contains only the set bits if (i < 0) num = "1" + num; // final binary representation // of the required integer return num; } // Driver program to test above int main() { string num = "10011" ; cout << "Binary representation of next number = " << nextGreater(num); return 0; } |
Java
// Java implementation to find the binary // representation of next greater integer class GFG { // function to find the required // binary representation static String nextGreater(String num) { int l = num.length(); int i; // examine bits from the right for (i = l - 1 ; i >= 0 ; i--) { // if '0' is encountered, convert // it to '1' and then break if (num.charAt(i) == '0' ) { num = num.substring( 0 , i) + '1' + num.substring(i+ 1 ); break ; } // else convert '1' to '0' else { num = num.substring( 0 , i) + '0' + num.substring(i + 1 ); } // num[i] = '0'; } // if the binary representation // contains only the set bits if (i < 0 ) { num = "1" + num; } // final binary representation // of the required integer return num; } // Driver program to test above public static void main(String[] args) { String num = "10011" ; System.out.println( "Binary representation of next number = " + nextGreater(num)); } } //this code contributed by Rajput-Ji |
Python3
# Python3 implementation to find the binary # representation of next greater integer # function to find the required # binary representation def nextGreater(num1): l = len (num1); num = list (num1); # examine bits from the right i = l - 1 ; while (i > = 0 ): # if '0' is encountered, convert # it to '1' and then break if (num[i] = = '0' ): num[i] = '1' ; break ; # else convert '1' to '0' else : num[i] = '0' ; i - = 1 ; # if the binary representation # contains only the set bits num1 = ''.join(num); if (i < 0 ): num1 = '1' + num1; # final binary representation # of the required integer return num1; # Driver Code num = "10011" ; print ( "Binary representation of next number = " ,nextGreater(num)); # This code is contributed by mits |
C#
// C# implementation to find the binary // representation of next greater integer using System; public class GFG { // function to find the required // binary representation static String nextGreater(String num) { int l = num.Length; int i; // examine bits from the right for (i = l - 1; i >= 0; i--) { // if '0' is encountered, convert // it to '1' and then break if (num[i] == '0' ) { num = num.Substring(0, i) + '1' + num.Substring(i+1); break ; } // else convert '1' to '0' else { num = num.Substring(0, i) + '0' + num.Substring(i + 1); } // num[i] = '0'; } // if the binary representation // contains only the set bits if (i < 0) { num = "1" + num; } // final binary representation // of the required integer return num; } // Driver program to test above public static void Main() { String num = "10011" ; Console.WriteLine( "Binary representation of next number = " + nextGreater(num)); } } //this code contributed by Rajput-Ji |
Javascript
<script> // Javascript implementation to find the binary // representation of next greater integer // function to find the required // binary representation function nextGreater(num) { let l = num.length; let i; // examine bits from the right for (i = l - 1; i >= 0; i--) { // if '0' is encountered, convert // it to '1' and then break if (num[i] == '0' ) { num = num.substring(0, i) + '1' + num.substring(i+1); break ; } // else convert '1' to '0' else { num = num.substring(0, i) + '0' + num.substring(i + 1); } // num[i] = '0'; } // if the binary representation // contains only the set bits if (i < 0) { num = "1" + num; } // final binary representation // of the required integer return num; } // Driver program to test above let num = "10011" ; document.write( "Binary representation of next number = " + nextGreater(num) ); // This code is contributed by rag2127 </script> |
PHP
<?php // PHP implementation to find the binary // representation of next greater integer // function to find the required // binary representation function nextGreater( $num ) { $l = strlen ( $num ); // examine bits from the right for ( $i = $l - 1; $i >= 0; $i --) { // if '0' is encountered, convert // it to '1' and then break if ( $num [ $i ] == '0' ) { $num [ $i ] = '1' ; break ; } // else convert '1' to '0' else $num [ $i ] = '0' ; } // if the binary representation // contains only the set bits if ( $i < 0) $num = "1" . $num ; // final binary representation // of the required integer return $num ; } // Driver Code $num = "10011" ; echo "Binary representation of next number = " . nextGreater( $num ); // This code is contributed by ita_c ?> |
Binary representation of next number = 10100
Time Complexity: O(n) where n is the number of bits in the input.
Auxiliary Space: O(n), since the string gets copied when we pass it to a function.
Approach 2:
Step by step implementation:
- The nextGreater function takes a binary string num as input.
- The binary string is converted to an integer using the bitset class. The to_ulong method of the bitset class returns the integer representation of the binary string.
- The integer is incremented by 1.
- The incremented integer is converted back to a binary string using the bitset class. The to_string method of the bitset class returns the binary string representation of the integer.
- The resulting binary string may have leading zeros, so these are removed using the erase and find_first_not_of methods of the string class.
- The resulting binary string is returned.
C++
#include <iostream> #include <bitset> using namespace std; string nextGreater(string num) { // Convert binary string to integer bitset<32> b(num); int n = b.to_ulong(); // Increment integer by 1 n++; // Convert integer back to binary string string result = bitset<32>(n).to_string(); // Remove leading zeros result.erase(0, result.find_first_not_of( '0' )); return result; } int main() { string num = "10011" ; cout << "Binary representation of next number = " << nextGreater(num); return 0; } |
Java
import java.util.BitSet; public class Main { public static String nextGreater(String num) { // Convert binary string to integer BitSet b = new BitSet( 32 ); for ( int i = 0 ; i < num.length(); i++) { if (num.charAt(i) == '1' ) { b.set(num.length() - 1 - i); } } long n = b.toLongArray()[ 0 ]; // Increment integer by 1 n++; // Convert long back to binary string String result = Long.toBinaryString(n); // Remove leading zeros result = result.replaceFirst( "^0+(?!$)" , "" ); return result; } // Driver code public static void main(String[] args) { String num = "10011" ; System.out.println( "Binary representation of next number = " + nextGreater(num)); } } |
Python3
def nextGreater(num): # Convert binary string to integer n = int (num, 2 ) # Increment integer by 1 n + = 1 # Convert integer back to binary string result = bin (n)[ 2 :] # Remove leading zeros result = result.lstrip( '0' ) return result def main(): num = "10011" print ( "Binary representation of next number =" , nextGreater(num)) if __name__ = = "__main__" : main() |
C#
using System; class Program { // Function to find the next greater binary // representation static string NextGreaterBinary( string num) { // Convert binary string to integer int n = Convert.ToInt32(num, 2); // Increment integer by 1 n++; // Convert integer back to binary string string result = Convert.ToString(n, 2); // Remove leading zeros result = result.TrimStart( '0' ); return result; } static void Main() { string num = "10011" ; string nextBinary = NextGreaterBinary(num); Console.WriteLine( "Binary representation of next number = " + nextBinary); } } |
Javascript
// JavaScript Code for the above approach function nextGreater(num) { // Convert binary string to integer const n = parseInt(num, 2); // Increment integer by 1 const nextNum = n + 1; // Convert integer back to binary string let result = nextNum.toString(2); // Remove leading zeros result = result.replace(/^0+/, '' ); return result; } // Driver code const num = "10011" ; console.log( "Binary representation of next number =" , nextGreater(num)); // THIS CODE IS CONTRIBUTED BY PIYUSH AGARWAL |
Binary representation of next number = 10100
Time Complexity: O(n)
Auxiliary Space: O(1)
Explanation:
The time complexity of this approach is O(n), where n is the length of the input binary string. This is because each operation (conversion to integer, incrementing, conversion to binary string, and removing leading zeros) takes O(n) time.
The auxiliary space complexity of this approach is O(1), because only a constant amount of extra space is used (for variables such as b, n, and result).
Contact Us