Saturday, December 28, 2024
Google search engine
HomeData Modelling & AISmallest perfect cube in an array

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 is the only perfect cube in the array

Input: arr[] = {27, 8, 1, 64} 
Output:
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++




// 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;
}


Java




// 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..


Python3




# 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


C#




// 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..


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function that returns true
// if n is a perfect cube
function 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 return the smallest perfect
// cube from the array
function smallestPerfectCube(a, n)
{
 
    // Stores the minimum of all the
    // perfect cubes from the array
    let mini = Number.MAX_VALUE;
 
    // Traverse all elements in the array
    for (let 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
let a = [ 16, 8, 25, 2, 3, 10 ];
 
let n = a.length;
 
document.write(smallestPerfectCube(a, n));
 
</script>


Output: 

8

 

Time Complexity: O(n log n) because the inbuilt cbrt function is used
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments