Largest number in an array that is not a perfect cube
Given an array of n integers. The task is to find the largest number which is not a perfect cube. Print -1 if there is no number that is a perfect cube.
Examples:
Input: arr[] = {16, 8, 25, 2, 3, 10}
Output: 25
25 is the largest number that is not a perfect cube.
Input: arr[] = {36, 64, 10, 16, 29, 25}
Output: 36
A Simple Solution is to sort the elements and sort the numbers and start checking from back for a non-perfect cube number using cbrt() function. The first number from the end which is not 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 non-perfect cubes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkPerfectcube( int n)
{
int d = cbrt(n);
if (d * d * d == n)
return true ;
return false ;
}
int largestNonPerfectcubeNumber( int a[], int n)
{
int maxi = -1;
for ( int i = 0; i < n; i++) {
if (!checkPerfectcube(a[i]))
maxi = max(a[i], maxi);
}
return maxi;
}
int main()
{
int a[] = { 16, 64, 25, 2, 3, 10 };
int n = sizeof (a) / sizeof (a[0]);
cout << largestNonPerfectcubeNumber(a, n);
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
int max( int a, int b)
{
int max = a;
if (max < b)
max = b;
return max;
}
bool checkPerfectcube( int n)
{
int d = cbrt(n);
if (d * d * d == n)
return true ;
return false ;
}
int largestNonPerfectcubeNumber( int a[], int n)
{
int maxi = -1;
for ( int i = 0; i < n; i++) {
if (!checkPerfectcube(a[i]))
maxi = max(a[i], maxi);
}
return maxi;
}
int main()
{
int a[] = { 16, 64, 25, 2, 3, 10 };
int n = sizeof (a) / sizeof (a[0]);
printf ( "%d" ,largestNonPerfectcubeNumber(a, n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean checkPerfectcube( int n)
{
int d = ( int )Math.cbrt(n);
if (d * d * d == n)
return true ;
return false ;
}
static int largestNonPerfectcubeNumber( int []a, int n)
{
int maxi = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (!checkPerfectcube(a[i]))
maxi = Math.max(a[i], maxi);
}
return maxi;
}
public static void main (String[] args) {
int a[] = { 16 , 64 , 25 , 2 , 3 , 10 };
int n = a.length;
System.out.print( largestNonPerfectcubeNumber(a, n));
}
}
|
Python 3
import math
def checkPerfectcube(n):
cube_root = n * * ( 1. / 3. )
if round (cube_root) * * 3 = = n:
return True
else :
return False
def largestNonPerfectcubeNumber(a, n):
maxi = - 1
for i in range ( 0 , n, 1 ):
if (checkPerfectcube(a[i]) = = False ):
maxi = max (a[i], maxi)
return maxi
if __name__ = = '__main__' :
a = [ 16 , 64 , 25 , 2 , 3 , 10 ]
n = len (a)
print (largestNonPerfectcubeNumber(a, n))
|
C#
using System;
public class GFG {
static bool checkPerfectcube( int n)
{
int d = ( int )Math.Ceiling(Math.Pow(n, ( double )1 / 3));
if (d * d * d == n)
return true ;
return false ;
}
static int largestNonPerfectcubeNumber( int []a, int n)
{
int maxi = -1;
for ( int i = 0; i < n; i++) {
if (checkPerfectcube(a[i])== false )
maxi = Math.Max(a[i], maxi);
}
return maxi;
}
public static void Main () {
int []a = { 16, 64, 25, 2, 3, 10 };
int n = a.Length;
Console.WriteLine( largestNonPerfectcubeNumber(a, n));
}
}
|
PHP
<?php
function checkPerfectcube( $n )
{
$d = (int) round (pow( $n , 1/3));
if ( $d * $d * $d == $n )
return true;
return false;
}
function largestNonPerfectcubeNumber( $a , $n )
{
$maxi = -1;
for ( $i = 0; $i < $n ; $i ++) {
if (!checkPerfectcube( $a [ $i ]))
$maxi = max( $a [ $i ], $maxi );
}
return $maxi ;
}
$a = array ( 16, 64, 25, 2, 3, 10 );
$n = count ( $a );
echo largestNonPerfectcubeNumber( $a , $n );
?>
|
Javascript
<script>
function checkPerfectcube(n)
{
let d = Math.cbrt(n);
if (d * d * d == n)
return true ;
return false ;
}
function largestNonPerfectcubeNumber(a, n)
{
let maxi = -1;
for (let i = 0; i < n; i++) {
if (!checkPerfectcube(a[i]))
maxi = Math.max(a[i], maxi);
}
return maxi;
}
let a = [ 16, 64, 25, 2, 3, 10 ];
let n = a.length;
document.write(largestNonPerfectcubeNumber(a, n));
</script>
|
Complexity Analysis:
- Time Complexity : O(nlog3(val)), since there runs a loop from 0 to (n – 1) where val is the max value of the array.
- Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
28 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...