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++
// 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.. |
8
Recommended Posts:
- Smallest perfect Cube divisible by all elements of an array
- Largest number in an array that is not a perfect cube
- Largest perfect cube number in an Array
- Count numbers upto N which are both perfect square and perfect cube
- Smallest perfect square divisible by all elements of an array
- Perfect cube greater than a given number
- Smallest and Largest N-digit perfect cubes
- Smallest and Largest N-digit perfect squares
- Smallest N digit number which is a perfect fourth power
- Find the smallest and second smallest elements in an array
- Maximum sum of smallest and second smallest in an array
- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Check whether the given array is perfect or not
- Sum of all perfect numbers present in an array
- Largest sub-array whose all elements are perfect squares
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.


