Cube root is an integer value that, when multiplied by itself three times in succession, results in the original value. In this article, we will write a Java program that uses binary search to find the cube root of a number. Finding the cube root of a number is an application of the binary search algorithm. In this article, we will discuss in detail how to use binary search to calculate cube roots.
Input-output example
Example-1: Input: 64 Output: 4
For example, the cube root of 64 is 4, and the output is 4.
Example-2: Input: 216 Output: 6
For example, the cube root of 216 is 6, and the output is 6.
Binary search
Binary search is an algorithm used to find elements (i.e. keys in a sorted array). The working principle of binary algorithm is as follows
Assume the array is "arr". Sort an array in ascending or descending order.
Initialize low = 0 and high = n-1 (n = number of elements), and calculate mid as middle = low (high-low)/2. If arr[middle] == key then returns middle, the middle index of the array.
If the key value is less than the arr[middle] element, set the high index to the middle index -1; if the key value is greater than the middle element, set the low index to the middle index 1
Continue binary search until you find the element you want to find.
If low is greater than high, return false directly because the key value does not exist in the array 'arr'.
Example of using binary search to find a key
question
Given an ordered integer array arr = [1, 3, 5, 7, 9, 11], use binary search to find the index of the element, that is, key = 7.
solution
Initialize low = 0 and high = 5 (last index of the array).
The first iteration of the while loop gives the middle index mid = low (high-low)/2
Median = 0 (5-0)/2 = 2.
The value of arr[mid] is 5, which is less than the key value 7. Therefore, we update low= mid 1 = 3.
The second iteration of the while loop gives us the mid index mid = 4 by using low (high-low)/2.
The value of arr[mid] is 9, which is greater than the key value 7. Therefore, we update high = 3 (mid - 1).
The third iteration of the while loop gives us the mid index mid = 3.
arr[mid] is 7, equal to the key value. Therefore, we return the middle index, which is 3.
So, in the given array, the index of the keyword is 7, and we found the index 3 using the binary search algorithm.
Algorithm for finding cube roots using binary search
Step 1 - Consider a number 'n' and initialize low=0 and right=n (the given number).
Step 2 - Find the median of low and high values using mid = low (high-low)/2.
Step 3 − Find the value of mid * mid * mid. If mid * mid * mid == n, return the value of mid.
Step 4 - If the middle value is less than n, then low=mid 1, otherwise high=mid-1
Step 5 - Repeat steps 2 through 4 until you find the value.
The Chinese translation ofExample
is:Example
In this example, we use the binary search algorithm to find the cube root of a value. We created a custom class 'BinarySearchCbrt' and implemented the binary search code for finding the cube root of a number in the 'cuberoot' function. Now, create a custom class object and initialize an integer variable named 'number' and call the 'cuberoot' function using the class object, thereby displaying the desired output.
//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)
So, in this article we have discussed how to find the cube root of a number using binary search algorithm in Java.
The above is the detailed content of Java program to find the cube root of a number using binary search algorithm. For more information, please follow other related articles on the PHP Chinese website!

ThisarticleusesvariousapproachesforselectingthecommandsinsertedintheopenedcommandwindowthroughtheJavacode.Thecommandwindowisopenedbyusing‘cmd’.Here,themethodsofdoingthesamearespecifiedusingJavacode.TheCommandwindowisfirstopenedusingtheJavaprogram.Iti

Java语言是当今世界上最常用的面向对象编程语言之一。类的概念是面向对象语言中最重要的特性之一。一个类就像一个对象的蓝图。例如,当我们想要建造一座房子时,我们首先创建一份房子的蓝图,换句话说,我们创建一个显示我们将如何建造房子的计划。根据这个计划,我们可以建造许多房子。同样地,使用类,我们可以创建许多对象。类是创建许多对象的蓝图,其中对象是真实世界的实体,如汽车、自行车、笔等。一个类具有所有对象的特征,而对象具有这些特征的值。在本文中,我们将使用类的概念编写一个Java程序,以找到矩形的周长和面

文件的大小是特定文件在特定存储设备(例如硬盘驱动器)上占用的存储空间量。文件的大小以字节为单位来衡量。在本节中,我们将讨论如何实现一个java程序来获取给定文件的大小(以字节、千字节和兆字节为单位)。字节是数字信息的最小单位。一个字节等于八位。1千字节(KB)=1,024字节1兆字节(MB)=1,024KB千兆字节(GB)=1,024MB和1太字节(TB)=1,024GB。文件的大小通常取决于文件的类型及其包含的数据量。以文本文档为例,文件的大小可能只有几千字节,而高分辨率图像或视频文件的大小可

继承是一个概念,它允许我们从一个类访问另一个类的属性和行为。被继承方法和成员变量的类被称为超类或父类,而继承这些方法和成员变量的类被称为子类或子类。在Java中,我们使用“extends”关键字来继承一个类。在本文中,我们将讨论使用继承来计算定期存款和定期存款的利息的Java程序。首先,在您的本地机器IDE中创建这四个Java文件-Acnt.java−这个文件将包含一个抽象类‘Acnt’,用于存储账户详情,如利率和金额。它还将具有一个带有参数‘amnt’的抽象方法‘calcIntrst’,用于计

罗马数字-基于古罗马系统,使用符号来表示数字。这些数字称为罗马数字。符号为I、V、X、L、C、D和M,分别代表1、5、10、50、100、500和1,000。整数-整数就是由正值、负值和零值组成的整数。分数不是整数。这里我们根据整数值设置符号值。每当输入罗马数字作为输入时,我们都会将其划分为单位,然后计算适当的罗马数字。I-1II–2III–3IV–4V–5VI–6...X–10XI–11..XV-15在本文中,我们将了解如何在Java中将罗马数字转换为整数。向您展示一些实例-实例1InputR

如何使用C#编写二分查找算法二分查找算法是一种高效的查找算法,它在有序数组中查找特定元素的位置,时间复杂度为O(logN)。在C#中,我们可以通过以下几个步骤来编写二分查找算法。步骤一:准备数据首先,我们需要准备一个已经排好序的数组作为查找的目标数据。假设我们要在数组中查找特定元素的位置。int[]data={1,3,5,7,9,11,13

如果有人想在Java编程语言方面打下坚实的基础。然后,有必要了解循环的工作原理。此外,解决金字塔模式问题是增强Java基础知识的最佳方法,因为它包括for和while循环的广泛使用。本文旨在提供一些Java程序,借助Java中可用的不同类型的循环来打印金字塔图案。创建金字塔图案的Java程序我们将通过Java程序打印以下金字塔图案-倒星金字塔星金字塔数字金字塔让我们一一讨论。模式1:倒星金字塔方法声明并初始化一个指定行数的整数“n”。接下来,将空间的初始计数定义为0,将星形的初始计数定义为“n+

一个图像文件可以顺时针或逆时针旋转。要旋转图像,需要下载一个随机的图像文件并将其保存在系统的任何文件夹中。此外,需要一个.pdf文件,在打开下载的图像文件后,可以在该特定的.pdf文件中旋转一些角度。对于90度的旋转,新图像的锚点可以帮助我们使用Java中的平移变换执行旋转操作。锚点是任何特定图像的中心。AlgorithmtoRotateanImagebyUsingJavaThe"AffineTransformOp"classisthesimplestwaytorotatea


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
