You are given two complex numbers 'NUM1' and 'NUM2' as strings, “A+Bi” where ‘A’ represents the real part and ‘B’ represents the imaginary part.
You have to find the complex multiplication of given numbers and return it as a string “A+Bi”, where ‘A’ represents the real part of the result and ‘B’ represents the imaginary part of the result.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single string, representing the first complex number 'NUM1'.
The second line of each test case will contain a single string, representing the second complex number 'NUM2'.
Output Format:
For each test case, return the complex multiplication as a string of the form “A+Bi”.
Note:
You don’t need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 10^5
-10^7 <= A, B <= 10^7
Time Limit: 1 sec
2
3+2i
8+-1i
0+1i
-1+0i
26+13i
0+-1i
In the first test case,
First complex number 'num1' = 3 + 2i
Second complex number 'num2' =8 + (-1)i
‘result’= (3+ 2i) * (8 - i) = (24 - 3i) + (16i + 2 ) = 26+13i
For the second test case.
First complex number 'num1' = 0 + i
Second complex number 'num2' = -1 + 0 i
‘result’ = (i) * (-1) = -i = “0+-1i”
2
-101+100i
0+0i
1+1i
1+-1i
0+0i
2+0i
Can we derive a formula for the real and the imaginary part of the multiplication?
Let first and the second complex number be, ‘a + bi’ and ‘c+di’, then
'RESULT' = (a + bi) *(c + di) = (ac + adi) = (bci + bd * (-1)) as i^2 = -1
‘RESULT’ = (ac - bd) + (ad+ bc) i
The steps are as follows:
O(L), where 'L' is the maximum length number between 'num1' and 'num2'.
Since we have to traverse through the ‘num1’ and ‘num2’ to parse them into the integers (long long) and so, the overall time complexity will be O(L).
O(L), where 'L' is the maximum length number between 'num1' and 'num2'.
Since the length of the resultant string can be comparable to ‘num1’ and ‘num2’ and so, the overall space complexity will be O(L).