Multiplication of Two numbers

LEVEL:Beginner

Description

Given two integers. You have to perform multiplication operation between two numbers using bitwise operators.

Input Format

First line contains two integers separated by space.

Output Format

Print the resultant value.


Example 1:

Input
1 3
Output
3
Example 2:

Input
5 7
Output
35
Example 3:

Input
10 12
Output
120

oops

Login to see Discussion




Approach


Approach 1:

Step-i)get the minimum of both the numbers
Step-ii) assign minimum number to num1 and other number to num2
Step-iii)initialize a variable to 0
Step-iv) if and operation between num2 and 1 is 1 add num1 to the variable
Step-v)right shift bits of num1 by one position and left shift bits of num2 by one position
Step-vi) repeat steps iv and v until num2 is greater than 0
Step-vii) return answer

Time Complexity: O(logn) where n is maximum of two numbers
Space Complexity: O(1)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution