Smallest perfect cube in an array

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 then 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns true
// if n is a perfect cube
bool 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 return the smallest perfect
// cube from the array
int smallestPerfectCube(int a[], int n)
{
  
    // Stores the minimum of all the
    // perfect cubes from the array
    int mini = INT_MAX;
  
    // Traverse all elements in the array
    for (int i = 0; i < n; i++) {
  
        // Store the minimum if current
        // element is a perfect cube
        if (checkPerfectcube(a[i])) {
            mini = min(a[i], mini);
        }
    }
  
    return mini;
}
  
// Driver code
int main()
{
    int a[] = { 16, 8, 25, 2, 3, 10 };
  
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << smallestPerfectCube(a, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG 
{
  
// Function that returns true
// if n is a perfect cube
static 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 return the smallest perfect
// cube from the array
static int smallestPerfectCube(int a[], int n)
{
  
    // Stores the minimum of all the
    // perfect cubes from the array
    int mini = Integer.MAX_VALUE;
  
    // Traverse all elements in the array
    for (int i = 0; i < n; i++) 
    {
  
        // Store the minimum if current
        // element is a perfect cube
        if (checkPerfectcube(a[i]))
        {
            mini = Math.min(a[i], mini);
        }
    }
  
    return mini;
}
  
// Driver code
public static void main (String[] args) 
{
    int a[] = { 16, 8, 25, 2, 3, 10 };
  
    int n = a.length;
  
    System.out.print(smallestPerfectCube(a, n));
}
}
  
// This code is contributed by anuj_67..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
import sys
  
# Function that returns true 
# if n is a perfect cube 
def checkPerfectcube(n) :
      
    # Takes the sqrt of the number 
    d = int(n**(1/3)); 
  
    # Checks if it is a perfect 
    # cube number 
    if (d * d * d == n) :
        return True
  
    return False
  
# Function to return the smallest perfect 
# cube from the array 
def smallestPerfectCube(a, n) : 
  
    # Stores the minimum of all the 
    # perfect cubes from the array 
    mini = sys.maxsize; 
  
    # Traverse all elements in the array 
    for i in range(n) :
  
        # Store the minimum if current 
        # element is a perfect cube 
        if (checkPerfectcube(a[i])) :
            mini = min(a[i], mini); 
              
    return mini; 
  
  
# Driver code 
if __name__ == "__main__"
  
    a = [ 16, 8, 25, 2, 3, 10 ]; 
  
    n = len(a); 
  
    print(smallestPerfectCube(a, n)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
  
    // Function that returns true
    // if n is a perfect cube
    static bool checkPerfectcube(int n)
    {
        // Takes the sqrt of the number
        int d = (int)Math.Sqrt(n);
      
        // Checks if it is a perfect
        // cube number
        if (d * d * d == n)
            return true;
      
        return false;
    }
      
    // Function to return the smallest perfect
    // cube from the array
    static int smallestPerfectCube(int []a, int n)
    {
      
        // Stores the minimum of all the
        // perfect cubes from the array
        int mini = int.MaxValue;
      
        // Traverse all elements in the array
        for (int i = 0; i < n; i++) 
        {
      
            // Store the minimum if current
            // element is a perfect cube
            if (checkPerfectcube(a[i]))
            {
                mini = Math.Min(a[i], mini);
            }
        }
      
        return mini;
    }
      
    // Driver code
    static public void Main ()
    {
        int []a = { 16, 8, 25, 2, 3, 10 };
      
        int n = a.Length;
        Console.Write(smallestPerfectCube(a, n));
    }
}
  
// This code is contributed by ajit..

chevron_right


Output:

8


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, jit_t, AnkitRai01