In this video, we will write a C Program to find the cube root of a negative number. Generally, a negative number is a number whose value is always less than zero and it has a minus (-) sign before it.
Examples: -12, -9
Basically, a cube is a result of multiplying the same integer three times.
i.e cube = N*N*N
Examples of Negative number cube roots:
Input: n = -27
Output: -3
In the video to find a cube root of a negative number we use 2 different methods:
1. Using binary search
2. Using pow(number,1.0/3.0)
Using Binary Search:
Step 1: Initialize start = 0 and end = n,e=0.0000001
Step 2:Calculate mid = (start + end)/2
Step 3:Take a variable error and check if the absolute value of (n – mid*mid*mid) < e. If this condition holds true then mid is our answer so return mid.
Step 4:If (mid*mid*mid)>n then set end=mid.
Step 5:If (mid*mid*mid)
Using POW(): Here we use math.h pow() function to find a cube root of a given negative number.
Apart from that, we will see the time and space complexity of both methods,i.e
1. Using Binary Search:
Time Complexity: O(logN)
Space Complexity: O(1)
2. Using POW():
Time Complexity: O(log(n))
Space Complexity: O(1)