Related Articles

# Smallest perfect cube in an array

• Difficulty Level : Easy
• Last Updated : 15 Apr, 2021

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

 `// C++ implementation of the approach``#include ``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);` `    ``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

 ``
Output:
`8`

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.

My Personal Notes arrow_drop_up