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++ code to implement the above approach #include <bits/stdc++.h> using namespace std;
// Function to output A and B void func(string& s)
{ // Variable to count the
// number of set bits in String S
int count = 0;
for ( char ch : s) {
if (ch == '1' ) {
count++;
}
}
// If S is the binary representation
// of a power of 2
if (count == 0) {
cout << "0 0\n" ;
}
else if (count == 1) {
cout << s << " 0\n" ;
}
else {
// Two strings to hold the
// binary representation of A and B
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;
}
}
// Construct A
A += '1' ;
for ( int i = s.length() - 1; i >= Y; i--)
A += '0' ;
// Construct B
B += '1' ;
for ( int i = s.length() - 1; i > X; i--)
B += '0' ;
cout << A << " " << B << "\n" ;
}
} // Driver Function int main()
{ // Input
string S = "11110000" ;
// Function call
func(S);
return 0;
} // This code is contributed by Abhinav Mahajan (abhinav_m22) |
// Java code to implment the approach import java.util.*;
// Driver Class class Main {
// Driver Fcuntion
public static void main(String[] args)
{
// Input
String S = " 11110000 ";
// function call
func(S);
}
// Method to output A and B
public static void func(String s)
{
// Variable to count the
// No. of set bits in String S
int count = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if (s.charAt(i) == '1' ) {
count++;
}
}
// If S is binary representation
// of Power of 2
if (count == 0 ) {
System.out.println(" 0 0 ");
}
else if (count == 1 ) {
System.out.println(s + " " + 0 );
}
else {
// Two StringBuilder to hold the
// Binary String of A and B
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;
}
}
// Construct A
A.append(" 1 ");
for ( int i = s.length() - 1 ; i >= Y; i--)
A.append(" 0 ");
// Construct B
B.append(" 1 ");
for ( int i = s.length() - 1 ; i > X; i--)
B.append(" 0 ");
System.out.println(A + " " + B);
}
}
} |
# code by FLutterfly def func(s):
# Variable to count the number of set bits in String S
count = s.count( '1' )
# If S is the binary representation of a power of 2
if count = = 0 :
print ( "0 0" )
elif count = = 1 :
print (s + " 0" )
else :
# Two strings to hold the binary representation of A and B
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
# Construct A
A + = '1' + '0' * ( len (s) - Y)
# Construct B
B + = '1' + '0' * ( len (s) - X - 1 )
print (A, B)
# Driver Function if __name__ = = "__main__" :
# Input
S = "11110000"
# Function call
func(S)
|
//code by Flutterfly using System;
class Program
{ // Function to output A and B
static void Func( string s)
{
// Variable to count the number of set bits in String S
int count = 0;
foreach ( char ch in s)
{
if (ch == '1' )
{
count++;
}
}
// If S is the binary representation of a power of 2
if (count == 0)
{
Console.WriteLine( "0 0" );
}
else if (count == 1)
{
Console.WriteLine($ "{s} 0" );
}
else
{
// Two strings to hold the binary representation of A and B
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;
}
}
// Construct A
A += '1' ;
for ( int i = s.Length - 1; i >= Y; i--)
A += '0' ;
// Construct B
B += '1' ;
for ( int i = s.Length - 1; i > X; i--)
B += '0' ;
Console.WriteLine($ "{A} {B}" );
}
}
// Driver Function
static void Main()
{
// Input
string S = "11110000" ;
// Function call
Func(S);
}
} |
// Function to output A and B function func(s) {
// Variable to count the number of set bits in String S
let count = 0;
for (let i = 0; i < s.length; i++) {
if (s[i] === '1' ) {
count++;
}
}
// If S is the binary representation of a power of 2
if (count === 0) {
console.log( "0 0" );
} else if (count === 1) {
console.log(`${s} 0`);
} else {
// Two strings to hold the binary representation of A and B
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;
}
}
}
// Construct A
A += '1' ;
for (let i = s.length - 1; i >= Y; i--) {
A += '0' ;
}
// Construct B
B += '1' ;
for (let i = s.length - 1; i > X; i--) {
B += '0' ;
}
console.log(`${A} ${B}`);
}
} // Driver Function const S = "11110000" ;
// Function call func(S); |
100000000 10000
Time Complexity: O(N), where N is the length of the string S.
Auxiliary Space: O(1)