Java Program to Find the Cube Root of a Given Number Using Binary Search Last Updated : 03 Mar, 2021 Comments Improve Suggest changes Like Article Like Report Given a non-negative number find the cube root of a number using the binary search approach. Examples : Input: x = 27 Output: 3 Explanation: The cube root of 16 is 4. Input: x = 120 Output: 4 Explanation: The cube root of 120 lies in between 4 and 5 so floor of the cube root is 4.Naive Approach: Check the cube of every element till n and store the answer till the cube is smaller or equal to the n Java // Java Program to Find the cube root // of given number using Naive approach import java.io.*; class GFG { static int cuberoot(int n) { int ans = 0; for (int i = 1; i <= n; ++i) { // checking every number cube if (i * i * i <= n) { ans = i; } } return ans; } public static void main(String[] args) { // Number int number = 27; // Checking number int cuberoot = cuberoot(number); System.out.println(cuberoot); } } Output3 Complexity: SpaceComplexity: O(1) TimeComplexity: O(n)Efficient Approach (Binary Search): Binary Search used Divide and Conquer approach that makes the complexity is O(log n). Algorithm: Initialize left=0 and right =nCalculate mid=left+(right-left)/2If mid*mid*mid is equal to the number return the midIf mid*mid*mid is less than the number store the mid in ans and increase left=mid+1If mid*mid*mid is more than the number and decrease the right=mid-1Return the answerImplementation: Java // Java Program to Find the cube root // of given number using Binary Search import java.io.*; import java.util.*; class GFG { // Function to find cuberoot static int cuberoot(int number) { // Lower bound int left = 1; // Upper bound int right = number; int ans = 0; while (left <= right) { // Finding the mid value int mid = left + (right - left) / 2; // Checking the mid value if (mid * mid * mid == number) { return mid; } // Shift the lower bound if (mid * mid * mid < number) { left = mid + 1; ans = mid; } // Shift the upper bound else { right = mid - 1; } } // Return the ans return ans; } public static void main(String[] args) { int number = 215; System.out.println(cuberoot(number)); } } Output5 Complexity: SpaceComplexity: O(1) TimeComplexity: O(log n) Comment More infoAdvertise with us Next Article Java Program to Find the Cube Root of a Given Number Using Binary Search zack_aayush Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Calculating the Power of a Number in Java Without Using Math pow() Method We need to calculate a number raised to the power of some other number without using the predefined function java.lang.Math.pow() In Java, we can calculate the power of any number by : Calculating the power of a number through while loop or for loop.Calculating the power of a number by the divide an 3 min read Check if a number is binary or not in Java Given a number N, the task is to check first whether the given number is binary or not and its value should be greater than 1. print true if N is the binary representation else print false. Examples: Input: N = 1010 Output: true Explanation: Given number is greater than 1 and none of its digits is g 3 min read Binary Search in Java Binary search is a highly efficient searching algorithm used when the input is sorted. It works by repeatedly dividing the search range in half, reducing the number of comparisons needed compared to a linear search. Here, we are focusing on finding the middle element that acts as a reference frame t 6 min read Sort and Search an Element in Java In Java sorting and searching an element in an array is very easy. Unlike C, where we have to make all the functions to work, Java has inbuilt functions to do the same work. To sort an array there is a sort function and to search an element in a sorted array there is a binarySearch() function. To le 3 min read JavaScript Program to Find Cube Root of a Number The cube root of a number n is a value x such that x3 = n. With JavaScript programming, we can implement an algorithm to find the cube root of a given number using various methods. The goal is to implement a function cube Root(x) that computes the cube root of a given number x using various approach 3 min read How to Find the Cube Roots of a Number? The cube root of a real number x is such a number that when multiplied twice by itself gives us the number x. In other terms, the result generated by multiplying a number x three times is known as the cube of that number. The cube root of the integer x3 is x. The sign âx or (x)1/3 represents a numbe 6 min read Python Program to Find Cube of a Number We are given a number and our task is to find the cube of this number in Python. The cube of a number is calculated by multiplying the number by itself twice. For example, if the input is 3, then the output will be 3 * 3 * 3 = 27. In this article, we will learn different ways to find the cube of a n 2 min read Find square root of a number using Bit Manipulation Given a non-negative integer N, the task is to find the square root of N using bitwise operations. If the integer is not the perfect square, return largest integer that is smaller than or equal to square root of N i.e., floor(âN). Examples: Input: N = 36Output: 6Explanation: The square root of 36 is 7 min read Searching in Binary Indexed Tree using Binary Lifting in O(LogN) Binary Indexed Tree (BIT) is a data structure that allows efficient queries of a range of elements in an array and updates on individual elements in O(log n) time complexity, where n is the number of elements in the array. Binary Lifting:One of the efficient techniques used to perform search operati 9 min read Java Program for cube sum of first n natural numbers Write a Java program to print the sum of series 13 + 23 + 33 + 43 + .......+ n3 till the n-th term. Examples: Input: n = 5Output: 225Explanation: 13 + 23 + 33 + 43 + 53 = 225 Input: n = 7Output: 784Explanation: 13 + 23 + 33 + 43 + 53 + 63 + 73 = 784 Java Program for cube sum of first n natural numbe 4 min read Like