Find two numbers whose difference is given as Binary string
Last Updated :
13 Dec, 2023
Given a string S, which contains a certain number of 1s followed by a certain number of 0s. Then your task is to output the binary representation of two integers A and B by following below conditions:
- A >= B
- The number of set bits in both A and B are minimum.
- A – B = K, where K is the Decimal representation of S.
Examples:
Input: S = 11100
Output: A = 100000, B = 100
Explanation: It is visible that A > B. Binary representation of A and B is 100000 and 100 respectively, which collectively have set bits equal to 2 and A – B = 32 – 4 = 28 which is Decimal representation of S.
Input: S = 1111100000
Output: A = 10000000000, B = 100000
Explanation: It is visible that A > B. Binary representation of A and B is 10000000000 and 100000 respectively, which collectively have set bits equal to 2 and A – B = 1024 – 32 = 992 which is Decimal representation of S.
Observations:
- If string S has only one 1, then we can have A = K and B = 0
- Otherwise, for every possible S, there will exist A and B such that A – B = K and A and B both are perfect powers of 2.
- Examples:
- S = 110 then A = 8 and B = 2
- S = 11110 then A = 32, B = 2
- S = 11110000 then A = 256, B = 32
Approach: Implement the idea below to solve the problem:
The problem can be solved using the above observations. Let’s say we start moving from right to left and X is the position of the first occurrence of 1 and Y is the position of last occurrence of 1, then we always say that A = (2 ^ (Y + 1)) and B = (2 ^ X) such that A – B = K.
Steps were taken to solve the problem:
- Declare a variable let say Count to count the number of set bits in S.
- Run a loop and count the number of set bits in S.
- If (Count == 1), then A = S, B = 0
- Else
- Start moving from right to left and find the first and last occurrence of 1 in S say X and Y respectively.
- After traversing the string, make A = (2 ^ (Y + 1)) and B = (2 ^ (X + 1))
- Print A and B
Code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void func(string& s)
{
int count = 0;
for ( char ch : s) {
if (ch == '1' ) {
count++;
}
}
if (count == 0) {
cout << "0 0\n" ;
}
else if (count == 1) {
cout << s << " 0\n" ;
}
else {
string A, B;
int X = -1, Y = -1;
for ( int i = s.length() - 1; i >= 0; i--) {
if (s[i] == '1' ) {
Y = i;
if (X == -1)
X = i;
}
}
A += '1' ;
for ( int i = s.length() - 1; i >= Y; i--)
A += '0' ;
B += '1' ;
for ( int i = s.length() - 1; i > X; i--)
B += '0' ;
cout << A << " " << B << "\n" ;
}
}
int main()
{
string S = "11110000" ;
func(S);
return 0;
}
|
Java
import java.util.*;
class Main {
public static void main(String[] args)
{
String S = " 11110000 ";
func(S);
}
public static void func(String s)
{
int count = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if (s.charAt(i) == '1' ) {
count++;
}
}
if (count == 0 ) {
System.out.println(" 0 0 ");
}
else if (count == 1 ) {
System.out.println(s + " " + 0 );
}
else {
StringBuilder A = new StringBuilder();
StringBuilder B = new StringBuilder();
int X = - 1 , Y = - 1 ;
for ( int i = s.length() - 1 ; i >= 0 ; i--) {
if (s.charAt(i) == '1' ) {
Y = i;
if (X == - 1 )
X = i;
}
}
A.append(" 1 ");
for ( int i = s.length() - 1 ; i >= Y; i--)
A.append(" 0 ");
B.append(" 1 ");
for ( int i = s.length() - 1 ; i > X; i--)
B.append(" 0 ");
System.out.println(A + " " + B);
}
}
}
|
Python
def func(s):
count = s.count( '1' )
if count = = 0 :
print ( "0 0" )
elif count = = 1 :
print (s + " 0" )
else :
A, B = " ", " "
X, Y = - 1 , - 1
for i in range ( len (s) - 1 , - 1 , - 1 ):
if s[i] = = '1' :
Y = i
if X = = - 1 :
X = i
A + = '1' + '0' * ( len (s) - Y)
B + = '1' + '0' * ( len (s) - X - 1 )
print (A, B)
if __name__ = = "__main__" :
S = "11110000"
func(S)
|
C#
using System;
class Program
{
static void Func( string s)
{
int count = 0;
foreach ( char ch in s)
{
if (ch == '1' )
{
count++;
}
}
if (count == 0)
{
Console.WriteLine( "0 0" );
}
else if (count == 1)
{
Console.WriteLine($ "{s} 0" );
}
else
{
string A = "" , B = "" ;
int X = -1, Y = -1;
for ( int i = s.Length - 1; i >= 0; i--)
{
if (s[i] == '1' )
{
Y = i;
if (X == -1)
X = i;
}
}
A += '1' ;
for ( int i = s.Length - 1; i >= Y; i--)
A += '0' ;
B += '1' ;
for ( int i = s.Length - 1; i > X; i--)
B += '0' ;
Console.WriteLine($ "{A} {B}" );
}
}
static void Main()
{
string S = "11110000" ;
Func(S);
}
}
|
Javascript
function func(s) {
let count = 0;
for (let i = 0; i < s.length; i++) {
if (s[i] === '1' ) {
count++;
}
}
if (count === 0) {
console.log( "0 0" );
} else if (count === 1) {
console.log(`${s} 0`);
} else {
let A = '' ;
let B = '' ;
let X = -1;
let Y = -1;
for (let i = s.length - 1; i >= 0; i--) {
if (s[i] === '1' ) {
Y = i;
if (X === -1) {
X = i;
}
}
}
A += '1' ;
for (let i = s.length - 1; i >= Y; i--) {
A += '0' ;
}
B += '1' ;
for (let i = s.length - 1; i > X; i--) {
B += '0' ;
}
console.log(`${A} ${B}`);
}
}
const S = "11110000" ;
func(S);
|
Time Complexity: O(N), where N is the length of the string S.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...