Java Program to Find the Cube Root of a Given Number Using Binary Search Last Updated : 23 Jul, 2025 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) Create Quiz Comment Z zack_aayush Follow 0 Improve Z zack_aayush Follow 0 Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like