给定一个n个整数的数组arr [] 。任务是从阵列中找到最小的理想立方体。如果数组中没有完美的立方体,则打印-1 。
例子:
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.
一个简单的解决方案是对元素进行排序,然后对数字进行排序,并使用cbrt()函数从头开始检查是否有理想的立方数。从头开始的第一个数字是一个完美的立方数,这是我们的答案。排序的复杂度为O(n log n) ,而cbrt()函数的复杂度为log n ,因此在最坏的情况下,复杂度为O(n log n) 。
一个有效的解决方案是对O(n)中的所有元素进行迭代,并每次与最小元素进行比较,并存储所有完美立方的最小值。
下面是上述方法的实现:
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[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
输出:
8