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.
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.
N = 24 = 6*4 = 6(1<<2) = “6<<2”
Input: A = 6, B = 10
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
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:
- Represent B as powers of 2.
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:
Time Complexity: O(log N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Calculate maximum value using '+' or '*' sign between two numbers in a string
- Reverse an array without using subtract sign ‘-‘ anywhere in the code
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Divide two integers without using multiplication, division and mod operator
- Divide two integers without using multiplication, division and mod operator | Set2
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Check if an encoding represents a unique binary string
- Check if a string represents a hexadecimal number or not
- Product of Complex Numbers using three Multiplication Operation
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Multiplication of two complex numbers given as strings
- To find sum of two numbers without using any operator
- Largest of two distinct numbers without using any conditional statements or operators
- Write you own Power without using multiplication(*) and division(/) operators
- Calculate 7n/8 without using division and multiplication operators
- Multiply a number with 10 without using multiplication operator
- Multiplying a variable with a constant without using multiplication operator
- String matching with * (that matches with any) in any of the two strings
- Check whether Array represents a Fibonacci Series or not
- Sum of alternating sign Squares of first N natural numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.