![]() |
VOOZH | about |
Given a complex number z, the task is to determine the modulus of this complex number. Note: Given a complex number z = a + ib the modulus is denoted by |z| and is defined as [latex]\left | z \right | = \sqrt{a^{2}+b^{2}}[/latex] Examples:
Input: z = 3 + 4i
Output: 5 |z| = (32 + 42)1/2 = (9 + 16)1/2 = 5Input: z = 6 - 8i
Output: 10
Explanation: |z| = (62 + (-8)2)1/2 = (36 + 64)1/2 = 10
Approach: For the given complex number z = x + iy:
If z = x +iy Real part = x Imaginary part = y
Square of Real part = x2 Square of Imaginary part = y2
Sum = Square of Real part + Square of Imaginary part = x2 + y2
[latex]\left | z \right | = \sqrt{x^{2}+y^{2}}[/latex]
Below is the implementation of the above approach:
5
Time Complexity: O(1)
Auxiliary Space: O(1)
As constant extra space is used