Given a binary string that represents binary representation of positive number n, the task is to find the binary representation of n+1. The binary input may or may not fit in an integer, so we need to return a string.
Examples:
Input: s = "10011"
Output: "10100"
Explanation:
Here n = (19)10 = (10011)2
next greater integer = (20)10 = (10100)2Input: s = "111011101001111111"
Output: "111011101010000000"
Approach:
The idea is to use the binary counting pattern where incrementing a number requires turning all trailing 1s to 0s and then changing the rightmost 0 to 1 (or adding a new leading 1 if all digits are 1s). This approach directly simulates the carry operation of binary addition without converting to decimal.
Step by step approach:
- Remove any leading zeros as they don't affect the number's value.
- Scan from right to left (least significant bit to most significant bit).
- Change all consecutive 1s from right to 0s until finding a 0.
- Change the first encountered 0 to 1 and stop the process.
- If no 0 is found (all bits are 1), prepend a new leading 1 and set all other bits to 0.
// C++ program to find Binary representation of next number
#include <bits/stdc++.h>
using namespace std;
// Function to find Binary representation of next number
string binaryNextNumber(string s) {
int n = s.length();
// Remove leading zeros
int i = 0;
while (i < n && s[i] == '0') {
i++;
}
// If string is all zeros, return "1"
if (i == n) {
return "1";
}
s = s.substr(i);
n = s.length();
// Start from the rightmost bit
for (i = n - 1; i >= 0; i--) {
if (s[i] == '0') {
// Convert first '0' from right to '1'
s[i] = '1';
break;
}
// Convert all 1's to 0 till first
// 0 from right is found
else {
s[i] = '0';
}
}
// If all bits are '1', add a '1' at the beginning
if (i == -1) {
s = "1" + s;
}
return s;
}
int main() {
string s = "10011";
cout << binaryNextNumber(s);
return 0;
}
// Java program to find Binary representation of next number
class Main {
// Function to find Binary representation of next number
static String binaryNextNumber(String s) {
int n = s.length();
// Remove leading zeros
int i = 0;
while (i < n && s.charAt(i) == '0') {
i++;
}
// If string is all zeros, return "1"
if (i == n) {
return "1";
}
s = s.substring(i);
n = s.length();
// Convert string to char array for mutability
char[] arr = s.toCharArray();
// Start from the rightmost bit
for (i = n - 1; i >= 0; i--) {
if (arr[i] == '0') {
// Convert first '0' from right to '1'
arr[i] = '1';
break;
}
// Convert all 1's to 0 till first
// 0 from right is found
else {
arr[i] = '0';
}
}
// If all bits are '1', add a '1' at the beginning
if (i == -1) {
return "1" + new String(arr);
}
return new String(arr);
}
public static void main(String[] args) {
String s = "10011";
System.out.println(binaryNextNumber(s));
}
}
# Python program to find Binary representation of next number
# Function to find Binary representation of next number
def binaryNextNumber(s):
n = len(s)
# Remove leading zeros
i = 0
while i < n and s[i] == '0':
i += 1
# If string is all zeros, return "1"
if i == n:
return "1"
s = s[i:]
n = len(s)
s = list(s)
allOnes = True
# Start from the rightmost bit
for i in range(n - 1, -1, -1):
if s[i] == '0':
# Convert first '0' from right to '1'
s[i] = '1'
allOnes = False
break
# Convert all 1's to 0 till first
# 0 from right is found
else:
s[i] = '0'
# If all bits are '1', add a '1' at the beginning
if allOnes == True:
s = ['1'] + s
return ''.join(s)
if __name__ == "__main__":
s = "10011"
print(binaryNextNumber(s))
// C# program to find Binary representation of next number
using System;
class GfG {
// Function to find Binary representation of next number
static string binaryNextNumber(string s) {
int n = s.Length;
// Remove leading zeros
int i = 0;
while (i < n && s[i] == '0') {
i++;
}
// If string is all zeros, return "1"
if (i == n) {
return "1";
}
s = s.Substring(i);
n = s.Length;
char[] arr = s.ToCharArray();
// Start from the rightmost bit
for (i = n - 1; i >= 0; i--) {
if (arr[i] == '0') {
// Convert first '0' from right to '1'
arr[i] = '1';
break;
}
// Convert all 1's to 0 till first
// 0 from right is found
else {
arr[i] = '0';
}
}
// If all bits are '1', add a '1' at the beginning
if (i == -1) {
return "1" + new string(arr);
}
return new string(arr);
}
static void Main(string[] args) {
string s = "10011";
Console.WriteLine(binaryNextNumber(s));
}
}
// JavaScript program to find Binary representation of next number
// Function to find Binary representation of next number
function binaryNextNumber(s) {
let n = s.length;
// Remove leading zeros
let i = 0;
while (i < n && s[i] === '0') {
i++;
}
// If string is all zeros, return "1"
if (i === n) {
return "1";
}
s = s.substring(i);
n = s.length;
let arr = s.split('');
// Start from the rightmost bit
for (i = n - 1; i >= 0; i--) {
if (arr[i] === '0') {
// Convert first '0' from right to '1'
arr[i] = '1';
break;
}
// Convert all 1's to 0 till first
// 0 from right is found
else {
arr[i] = '0';
}
}
// If all bits are '1', add a '1' at the beginning
if (i === -1) {
arr.unshift('1');
}
return arr.join('');
}
let s = "10011";
console.log(binaryNextNumber(s));
Output
10100
Time Complexity: O(n), where n is the length of string. The input string is traversed at most 3 times.
Auxiliary Space: O(n), due to substring.