Smallest perfect cube in an array
Last Updated :
24 Dec, 2022
Given an array arr[] of n integers. The task is to find the smallest perfect cube from the array. Print -1 if there is no perfect cube in the array.
Examples:
Input: arr[] = {16, 8, 25, 2, 3, 10}
Output: 8
8 is the only perfect cube in the array
Input: arr[] = {27, 8, 1, 64}
Output: 1
All elements are perfect cubes but 1 is the minimum of all.
A simple solution is to sort the elements and sort the numbers and start checking from start for a perfect cube number using cbrt() function. The first number from the beginning 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 in 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 minimum element and store the minimum of all 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 smallestPerfectCube( int a[], int n)
{
int mini = INT_MAX;
for ( int i = 0; i < n; i++) {
if (checkPerfectcube(a[i])) {
mini = min(a[i], mini);
}
}
return mini;
}
int main()
{
int a[] = { 16, 8, 25, 2, 3, 10 };
int n = sizeof (a) / sizeof (a[0]);
cout << smallestPerfectCube(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 smallestPerfectCube( int a[], int n)
{
int mini = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
if (checkPerfectcube(a[i])) {
mini = Math.min(a[i], mini);
}
}
return mini;
}
public static void main(String[] args)
{
int a[] = { 16 , 8 , 25 , 2 , 3 , 10 };
int n = a.length;
System.out.print(smallestPerfectCube(a, n));
}
}
|
Python3
import sys
def checkPerfectcube(n):
d = int (n * * ( 1 / 3 ))
if (d * d * d = = n):
return True
return False
def smallestPerfectCube(a, n):
mini = sys.maxsize
for i in range (n):
if (checkPerfectcube(a[i])):
mini = min (a[i], mini)
return mini
if __name__ = = "__main__" :
a = [ 16 , 8 , 25 , 2 , 3 , 10 ]
n = len (a)
print (smallestPerfectCube(a, n))
|
C#
using System;
class GFG {
static bool checkPerfectcube( int n)
{
int d = ( int )Math.Sqrt(n);
if (d * d * d == n)
return true ;
return false ;
}
static int smallestPerfectCube( int [] a, int n)
{
int mini = int .MaxValue;
for ( int i = 0; i < n; i++) {
if (checkPerfectcube(a[i])) {
mini = Math.Min(a[i], mini);
}
}
return mini;
}
static public void Main()
{
int [] a = { 16, 8, 25, 2, 3, 10 };
int n = a.Length;
Console.Write(smallestPerfectCube(a, n));
}
}
|
Javascript
<script>
function checkPerfectcube(n)
{
let d = parseInt(Math.cbrt(n));
if (d * d * d == n)
return true ;
return false ;
}
function smallestPerfectCube(a, n)
{
let mini = Number.MAX_VALUE;
for (let i = 0; i < n; i++) {
if (checkPerfectcube(a[i])) {
mini = Math.min(a[i], mini);
}
}
return mini;
}
let a = [ 16, 8, 25, 2, 3, 10 ];
let n = a.length;
document.write(smallestPerfectCube(a, n));
</script>
|
Time Complexity: O(n log n) because the inbuilt cbrt function is used
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...