Largest perfect cube number in an Array
Given an array of N integers. The task is to find the largest number which is a perfect cube. Print -1 if there is no number that is perfect cube.
Examples:
Input : arr[] = {16, 8, 25, 2, 3, 10}
Output : 25
Explanation: 25 is the largest number
that is a perfect cube.
Input : arr[] = {36, 64, 10, 16, 29, 25|
Output : 64
A Simple Solution is to sort the elements and sort the N numbers and start checking from back for a perfect cube number using cbrt() function. The first number from the end which is a perfect cube number is our answer. The complexity of sorting is O(n log n) and of cbrt() function is log n, so at the worst case the complexity is O(n log n).
An Efficient Solution is to iterate for all the elements in O(n) and compare every time with the maximum element, and store the maximum of all perfect cubes.
Below is the implementation of the above approach:
C++
// CPP program to find the largest perfect// cube number among n numbers#include <bits/stdc++.h>using namespace std;// Function to check if a number// is perfect cube number or notbool checkPerfectcube(int n){ // takes the sqrt of the number int d = cbrt(n); // checks if it is a perfect // cube number if (d * d * d == n) return true; return false;}// Function to find the largest perfect// cube number in the arrayint largestPerfectcubeNumber(int a[], int n){ // stores the maximum of all // perfect cube numbers int maxi = -1; // Traverse all elements in the array for (int i = 0; i < n; i++) { // store the maximum if current // element is a perfect cube if (checkPerfectcube(a[i])) maxi = max(a[i], maxi); } return maxi;}// Driver Codeint main(){ int a[] = { 16, 64, 25, 2, 3, 10 }; int n = sizeof(a) / sizeof(a[0]); cout << largestPerfectcubeNumber(a, n); return 0;} |
Java
// Java program to find the largest perfect// cube number among n numbersclass Solution{// Function to check if a number// is perfect cube number or notstatic boolean checkPerfectcube(int n){ // takes the sqrt of the number int d =(int) Math.cbrt(n); // checks if it is a perfect // cube number if (d * d * d == n) return true; return false;}// Function to find the largest perfect// cube number in the arraystatic int largestPerfectcubeNumber(int a[], int n){ // stores the maximum of all // perfect cube numbers int maxi = -1; // Traverse all elements in the array for (int i = 0; i < n; i++) { // store the maximum if current // element is a perfect cube if (checkPerfectcube(a[i])) maxi = Math.max(a[i], maxi); } return maxi;}// Driver Codepublic static void main(String args[]){ int a[] = { 16, 64, 25, 2, 3, 10 }; int n =a.length; System.out.print(largestPerfectcubeNumber(a, n));}}//contributed by Arnab Kundu |
Python3
# Python 3 program to find the largest# perfect cube number among n numbersimport math# Function to check if a number# is perfect cube number or notdef checkPerfectcube(n): # checks if it is a perfect # cube number cube_root = n**(1./3.) if round(cube_root) ** 3 == n: return True else: return False# Function to find the largest perfect# cube number in the arraydef largestPerfectcubeNumber(a, n): # stores the maximum of all # perfect cube numbers maxi = -1 # Traverse all elements in the array for i in range(0, n, 1): # store the maximum if current # element is a perfect cube if (checkPerfectcube(a[i])): maxi = max(a[i], maxi) return maxi;# Driver Codeif __name__ == '__main__': a = [16, 64, 25, 2, 3, 10] n = len(a) print(largestPerfectcubeNumber(a, n))# This code is contributed by# Surendra_Gangwar |
C#
//C# program to find the largest perfect// cube number among n numbersusing System;public class Solution{ // Function to check if a number // is perfect cube number or not static bool checkPerfectcube(int n) { // takes the sqrt of the number int d = (int)Math.Ceiling(Math.Pow(n, (double)1 / 3)); // checks if it is a perfect // cube number if (d * d * d == n) return true; return false; } // Function to find the largest perfect // cube number in the array static int largestPerfectcubeNumber(int []a, int n) { // stores the maximum of all // perfect cube numbers int maxi = -1; // Traverse all elements in the array for (int i = 0; i < n; i++) { // store the maximum if current // element is a perfect cube if (checkPerfectcube(a[i])) maxi = Math.Max(a[i], maxi); } return maxi; } // Driver Code public static void Main() { int []a = { 16, 64, 25, 2, 3, 10 }; int n =a.Length; Console.WriteLine(largestPerfectcubeNumber(a, n)); }}/*This code is contributed by PrinciRaj1992*/ |
PHP
<?php// PHP program to find the largest perfect// cube number among n numbers// Function to check if a number// is perfect cube number or notfunction checkPerfectcube($n){ // takes the sqrt of the number $d = pow($n, (1 / 3)); $d = round($d); // checks if it is a perfect // cube number if ($d * $d * $d == $n) return true; return false;}// Function to find the largest perfect// cube number in the arrayfunction largestPerfectcubeNumber(&$a, $n){ // stores the maximum of all // perfect cube numbers $maxi = -1; // Traverse all elements in the array for ($i = 0; $i < $n; $i++) { // store the maximum if current // element is a perfect cube if (checkPerfectcube($a[$i])) $maxi = max($a[$i], $maxi); } return $maxi;}// Driver Code$a = array( 16, 64, 25, 2, 3, 10 );$n = sizeof($a);echo largestPerfectcubeNumber($a, $n);// This code is contributed by ita_c?> |
Javascript
<script>// Javascript program to find the largest perfect// cube number among n numbers// Function to check if a number// is perfect cube number or notfunction checkPerfectcube(n){ // takes the sqrt of the number let d = parseInt(Math.cbrt(n)); // checks if it is a perfect // cube number if (d * d * d == n) return true; return false;}// Function to find the largest perfect// cube number in the arrayfunction largestPerfectcubeNumber(a, n){ // stores the maximum of all // perfect cube numbers let maxi = -1; // Traverse all elements in the array for (let i = 0; i < n; i++) { // store the maximum if current // element is a perfect cube if (checkPerfectcube(a[i])) maxi = Math.max(a[i], maxi); } return maxi;}// Driver Codelet a = [ 16, 64, 25, 2, 3, 10 ];let n = a.length;document.write(largestPerfectcubeNumber(a, n));</script> |
64
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


