- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Find Cube Root of a number using Binary Search
Cube Root of a number is an integer value when multiplied by itself thrice, gives the original number. In this article, we are going to write a java program to find the cube root of a number using binary search. Finding cube root of a number is one of the application of the binary search algorithm. We will discuss in detail how we calculate the cube root using binary search in this article.
Input-Output Examples
Example-1: Input: 64 Output: 4
As, the cube root of 64 is 4, the output is 4.
Example-2: Input: 216 Output: 6
As, the cube root of 216 is 6, the output is 6.
Binary Search
Binary search is an algorithm used to find an element i.e., key in a sorted array. Binary algorithm works as below
Let us say that array is ‘arr’. Sort the array in ascending or descending order.
Initialize low = 0 and high = n-1 (n = number of elements) and calculate middle as middle = low + (high-low)/2. If arr[middle] == key then return middle i.e., middle index of array.
Else if the key value is less than the arr[middle] element set the high index as middle index-1 or if the key value is more than middle element set the low index as middle index+1
Continue the binary search until the element that needs to be find is found.
If low greater than high than return false directly as key is not present in the array ‘arr’.
Example to find Key using Binary Search
Problem
Given a sorted array of integers arr = [1, 3, 5, 7, 9, 11], find the index of the element i.e., key = 7 using binary search.
Solution
Initialize the low = 0 and the high= 5 (the last index of the array).
The first iteration of the while loop gives us the middle index mid = low+ (high-low)/2
mid = 0+(5-0)/2 = 2.
The value of arr[mid] is 5, which is less than the key value 7. So, we update the low= mid+1 = 3.
The second iteration of the while loop gives us the middle index mid = 4 by using the low+ (high-low)/2.
The value of arr[mid] is 9, which is greater than the key value 7. So, we update the high= 3 (mid - 1).
The third iteration of the while loop gives us the middle index mid = 3.
The arr[mid] is 7, which is equal to the key value. So, we return the middle index, which is 3.
Thus, the index of the key = 7 in the given array is 3, which we found using the binary search algorithm.
Algorithm to find the Cube Root using Binary Search
STEP 1 − Consider a number ‘n’ and initialise low=0 and right= n (given number).
STEP 2 − Find mid value of low and high using mid = low + (high-low)/2.
STEP 3 − find the value of mid * mid*mid, if mid * mid*mid == n then return mid value.
STEP 4 − If mid value is less than n then low=mid+1 else high =mid-1
STEP 5 − Repeat from steps 2 to 4 until we find the value.
Example
In this example, we find the cube root of a value using Binary Search Algorithm. We created a custom class ‘BinarySearchCbrt’ and implemented the binary search code for finding the cuberoot of a number code in ‘cuberoot’ function. Now, create the custom Class object and initialise a variable called ‘number’ with an integer number and using the class object class the cuberoot’ function is called and thus the desired output is displayed.
//Java Program to find Cube root of a number using Binary Search import java.util.*; class BinarySearchCbrt { public int cuberoot(int number) { int low = 0; int high = number; while (low <= high) { int mid = (low + high) / 2; int cube = mid * mid*mid; if (cube == number) { return mid; } else if (cube < number) { low = mid + 1; } else { high = mid - 1; } } return 0; } } public class Main { public static void main(String[] args) { int n = 64; BinarySearchCbrt Obj = new BinarySearchCbrt(); int result= Obj.cuberoot(n); System.out.println("Cube root of " + n + " = " + result); } }
Output
Cube root of 64 = 4
Time Complexity: O(NlogN) Auxiliary Space: O(1)
Thus, in this article we have discussed how to find the cube root of a number using Binary Search Algorithm in Java.