Last Updated: 18 Apr, 2021

Multiplying Complex Numbers

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

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

Approaches

01 Approach

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