Smallest string without any multiplication sign that represents the product of two given numbers
Last Updated :
10 Jun, 2021
Given two numbers A and B, the task is to print the string of the smallest possible length which evaluates to the product of the two given numbers i.e., A*B, without using the multiplication sign.
Any perfect power of 2 can be expressed in the form of a left-shift operator.
For Example:
N = 2 = 1<<1 = “<<1”
N = 4 = 1<<2 = “<<2”
Using the above idea, any number can be expressed using a left-shift operator instead of a multiplication sign.
For Example:
N = 24 = 6*4 = 6(1<<2) = “6<<2”
Examples:
Input: A = 6, B = 10
Output: 6<<3+6<<1
Explanation:
The product of the 2 numbers = 6 × 10 = 60.
The above-given expression evaluates to 6 × (2 × 2 × 2) + 6 × 2 = 60.
The string “10<<2+10<<1” also evaluates to 60.
But “6<<3+6<<1” is the required output as its length is smaller.
Input: A = 5, B = 5
Output: 5<<2+5
Explanation:
The product of the 2 numbers = 5 × 5 = 25.
The above-given expression evaluates to 5 × (2 × 2) + 5 = 25.
Approach: The idea is to use Left Shift Operator to find the product. Below are the steps:
Let B = 2k1 + 2k2 + … + 2kn, where k1 > k2 > .. > kn
- Therefore, the product of A and B can be written as
A * B = A * (2k1 + 2k2+ … + 2kn )
- Use the “<<“ (left shift operator) to multiply a number by any power of two.
- Thus A x B = A << k1 + A << k2 + … + A << kn
- To find ki we use the log() function and continue the process with the remainder B – 2ki until the remainder becomes 0 or the log of the remainder becomes zero.
- Similarly, represent A*B = B<< k1 + B<< k2 + … + B<< kn by representing A as the power of 2.
- Compare the two representations and print the string with a smaller length.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string len( long A, long B)
{
string res = "" ;
long Log = 0;
do
{
Log = ( long )( log (B) / log (2));
if (Log != 0)
{
res = res + to_string(A) +
"<<" + to_string(Log);
}
else
{
res += A;
break ;
}
B = B - ( long ) pow (2, Log);
if (B != 0)
{
res += "+" ;
}
else
break ;
} while (Log != 0);
return res;
}
void minimumString( long A, long B)
{
string res1 = len(A, B);
string res2 = len(B, A);
if (res1.length() > res2.length())
{
cout << res2 << endl;
}
else
{
cout << res1 << endl;
}
}
int main()
{
long A = 6;
long B = 10;
minimumString(A, B);
return 0;
}
|
Java
class GFG {
public static String len( long A,
long B)
{
String res = "" ;
long log = 0 ;
do {
log = ( long )(Math.log(B)
/ Math.log( 2 ));
if (log != 0 ) {
res += A + "<<" + log;
}
else {
res += A;
break ;
}
B = B - ( long )Math.pow( 2 , log);
if (B != 0 ) {
res += "+" ;
}
else
break ;
} while (log != 0 );
return res;
}
public static void
minimumString( long A, long B)
{
String res1 = len(A, B);
String res2 = len(B, A);
if (res1.length() > res2.length()) {
System.out.println(res2);
}
else {
System.out.println(res1);
}
}
public static void main(String args[])
{
long A = 6 ;
long B = 10 ;
minimumString(A, B);
}
}
|
Python3
from math import log
def lenn(A, B):
res = ""
logg = 0
while True :
logg = log(B) / / log( 2 )
if (logg ! = 0 ):
res + = ( str (A) + "<<" +
str ( int (logg)))
else :
res + = A
break
B = B - pow ( 2 , logg)
if (B ! = 0 ):
res + = "+"
else :
break
if logg = = 0 :
break
return res
def minimumString(A, B):
res1 = lenn(A, B)
res2 = lenn(B, A)
if ( len (res1) > len (res2)):
print (res2)
else :
print (res1)
if __name__ = = '__main__' :
A = 6
B = 10
minimumString(A, B)
|
C#
using System;
class GFG{
public static string len( long A, long B)
{
string res = "" ;
long log = 0;
do
{
log = ( long )(Math.Log(B) /
Math.Log(2));
if (log != 0)
{
res += A + "<<" + log;
}
else
{
res += A;
break ;
}
B = B - ( long )Math.Pow(2, log);
if (B != 0)
{
res += "+" ;
}
else
break ;
} while (log != 0);
return res;
}
public static void minimumString( long A,
long B)
{
string res1 = len(A, B);
string res2 = len(B, A);
if (res1.Length > res2.Length)
{
Console.WriteLine(res2);
}
else
{
Console.WriteLine(res1);
}
}
public static void Main()
{
long A = 6;
long B = 10;
minimumString(A, B);
}
}
|
Javascript
<script>
function len(A, B) {
let res = "" ;
let Log = 0;
do {
Log = Math.floor(Math.log(B) / Math.log(2));
if (Log != 0) {
res = res + String(A) + "<<" + String(Log);
}
else {
res += A;
break ;
}
B = B - Math.pow(2, Log);
if (B != 0) {
res += "+" ;
}
else
break ;
} while (Log != 0);
return res;
}
function minimumString(A, B) {
let res1 = len(A, B);
let res2 = len(B, A);
if (res1.length > res2.length) {
document.write(res2 + "<br>" );
}
else {
document.write(res1 + "<br>" );
}
}
let A = 6;
let B = 10;
minimumString(A, B);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...