Multiplying Complex Numbers

Easy
0/40
Average time to solve is 15m
profile
Contributed by
3 upvotes
Asked in companies
Deutsche BankJaguar Land Rover

Problem statement

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.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
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.
Constraints:
1 <= T <= 10^5
-10^7 <= A, B <= 10^7

Time Limit: 1 sec
Sample Input 1:
2
3+2i
8+-1i
0+1i
-1+0i
Sample Output 1:
26+13i
0+-1i
Explanation of sample input 1:
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”
Sample Input 2:
2
-101+100i
0+0i 
1+1i
1+-1i     
Sample Output 2:
0+0i
2+0i
Hint

Can we derive a formula for the real and the imaginary part of the multiplication?

Approaches (1)
Mathematics

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:

 

  1. We will parse the input and extract the real and imaginary parts of the number.
  2. Let ‘REAL1’, ‘IMG1’ store the real and imaginary part of the first number.
  3. Let ‘REAL2’, ‘IMG2’ store the real and the imaginary part of the second number.
  4. 'REAL_RESULT', 'IMG_RESULT' will store the real part and the imaginary part of the result.
  5. 'REAL_RESULT'= ‘REAL1’ * ‘REAL2’ - ‘IMG1’ * ’IMG2’
  6. 'IMG_RESULT' = ‘REAL1’ * ‘IMG2’ + ‘REAL2’ * ‘IMG1’
  7. 'RESULT' = 'REAL_RESULT' + ‘+’ + 'IMG_RESULT' + ‘i’
  8. Return 'RESULT'.
Time Complexity

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).

Space Complexity

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).

Code Solution
(100% EXP penalty)
Multiplying Complex Numbers
Full screen
Console