Complex Number Multiplication - Problem

Complex numbers are mathematical objects that extend real numbers by introducing the imaginary unit i, where i² = -1. They're widely used in engineering, physics, and computer graphics for representing rotations and oscillations.

In this problem, you're given two complex numbers as strings in the format "a+bi" or "a-bi", where:

  • a is the real part (integer between -100 and 100)
  • b is the imaginary part (integer between -100 and 100)
  • i represents the imaginary unit

Your task is to multiply these two complex numbers and return the result as a string in the same format.

Mathematical Formula: (a + bi) × (c + di) = (ac - bd) + (ad + bc)i

For example: "1+1i" × "1+1i" = "0+2i" because (1×1 - 1×1) + (1×1 + 1×1)i = 0 + 2i

Input & Output

example_1.py — Basic Multiplication
$ Input: num1 = "1+1i", num2 = "1+1i"
Output: "0+2i"
💡 Note: Using formula (a+bi)×(c+di) = (ac-bd)+(ad+bc)i: (1+1i)×(1+1i) = (1×1-1×1)+(1×1+1×1)i = 0+2i
example_2.py — Negative Result
$ Input: num1 = "1+-1i", num2 = "1+-1i"
Output: "0+-2i"
💡 Note: With negative imaginary parts: (1-1i)×(1-1i) = (1×1-(-1)×(-1))+(1×(-1)+(-1)×1)i = (1-1)+(-1-1)i = 0-2i
example_3.py — Pure Real Numbers
$ Input: num1 = "2+0i", num2 = "3+0i"
Output: "6+0i"
💡 Note: When imaginary parts are zero, it behaves like regular multiplication: (2+0i)×(3+0i) = (2×3-0×0)+(2×0+0×3)i = 6+0i

Visualization

Tap to expand
Complex Number Multiplication VisualizationRealImaginary(1+1i)(1+1i)Result: (0+2i)Formula CalculationGiven: (1+1i) × (1+1i)a=1, b=1, c=1, d=1Real part: ac - bd= 1×1 - 1×1 = 0Imaginary part: ad + bc= 1×1 + 1×1 = 2Result: 0 + 2i
Understanding the Visualization
1
Input Representation
Plot both complex numbers as points on the complex plane (real axis = x, imaginary axis = y)
2
Component Extraction
Identify the real and imaginary parts of each complex number
3
Formula Application
Apply the multiplication formula: real_result = ac-bd, imag_result = ad+bc
4
Result Plotting
Plot the resulting complex number and format as string output
Key Takeaway
🎯 Key Insight: Complex number multiplication is essentially a combination of algebraic expansion and the fundamental property that i² = -1, which transforms what could be a complex geometric operation into straightforward arithmetic.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Where n is the length of the input strings. We need to parse each character once to extract components.

n
2n
Linear Growth
Space Complexity
O(1)

Only using a constant amount of extra space to store the parsed components and result.

n
2n
Linear Space

Constraints

  • The real and imaginary parts are integers in the range [-100, 100]
  • The input strings will always be in the format "a+bi" or "a-bi"
  • The imaginary part will always end with the character 'i'
  • Both input strings represent valid complex numbers
Asked in
Google 25 Microsoft 18 Amazon 15 Meta 12
24.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen