Multiply two numbers of different base and represent product in another given base
Given two numbers N, M in the bases X, Y and another base P. The task is to find the product of N and M and represent the product in base P.
Input: N = 101, M = 110, X = 2, Y = 2, P = 16
Explanation: NX * MY = (101)2 * (110)2 = (11110)2
(11110)2 = (1E)16
Input: N = 101, M = A, X = 2, Y = 20, P = 16
Explanation: Nx = (101)2 = (5)20
NX * MY = (5)20 * (A)20 = (2A)20
(2A)20 = (32)16
Approach: The approach is to convert the given numbers in decimal, perform the product and then turn it back to a number of base p. Follow the steps mentioned below:
- Convert NX and MY to decimal number.
- Perform multiplication on the decimal numbers.
- Convert the result of multiplication from decimal to base P.
Below is the implementation of the above approach.
Time Complexity: O(D) where D is the maximum number of digits in N, M and product
Auxiliary Space: O(1)