Cubo perfecto más pequeño divisible por todos los elementos de una array

Dada una array arr[] , la tarea es encontrar el cubo perfecto más pequeño que sea divisible por todos los elementos de la array dada.
Ejemplos: 

Entrada: arr[] = {20, 4, 128, 7} 
Salida: 21952000
Entrada: arr[] = {10, 125, 14, 42, 100} 
Salida: 9261000 

Enfoque ingenuo: verifique todos los cubos perfectos uno por uno comenzando desde 1 y seleccione el que es divisible por todos los elementos de la array.
Enfoque eficiente: encuentre el mínimo común múltiplo de todos los elementos de la array y guárdelo en una variable mcm . Encuentre todos los factores primos del MCM encontrado. 
Ahora, para cada factor primo que divide el mcm ‘x’ el número de veces donde x % 3 != 0 :  

  • Si x% 3 = 2 , actualice lcm = lcm * hecho .
  • Si x % 3 = 1 , actualice lcm = lcm * hecho 2 .

Imprima el LCM actualizado al final.
A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long int
 
// Function to return the gcd of two numbers
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to return the lcm of
// all the elements of the array
ll lcmOfArray(int arr[], int n)
{
    if (n < 1)
        return 0;
 
    ll lcm = arr[0];
 
    // To calculate lcm of two numbers
    // multiply them and divide the result
    // by gcd of both the numbers
    for (int i = 1; i < n; i++)
        lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
 
    // Return the LCM of the array elements
    return lcm;
}
 
// Function to return the smallest perfect cube
// divisible by all the elements of arr[]
int minPerfectCube(int arr[], int n)
{
    ll minPerfectCube;
 
    // LCM of all the elements of arr[]
    ll lcm = lcmOfArray(arr, n);
    minPerfectCube = (long long)lcm;
 
    int cnt = 0;
    while (lcm > 1 && lcm % 2 == 0) {
        cnt++;
        lcm /= 2;
    }
 
    // If 2 divides lcm cnt number of times
    if (cnt % 3 == 2)
        minPerfectCube *= 2;
    else if (cnt % 3 == 1)
        minPerfectCube *= 4;
 
    int i = 3;
 
    // Check all the numbers that divide lcm
    while (lcm > 1) {
        cnt = 0;
        while (lcm % i == 0) {
            cnt++;
            lcm /= i;
        }
 
        if (cnt % 3 == 1)
            minPerfectCube *= i * i;
        else if (cnt % 3 == 2)
            minPerfectCube *= i;
 
        i += 2;
    }
 
    // Return the answer
    return minPerfectCube;
}
 
// Driver code
int main()
{
    int arr[] = { 10, 125, 14, 42, 100 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minPerfectCube(arr, n);
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the gcd of two numbers
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to return the lcm of
// aint the elements of the array
static int lcmOfArray(int arr[], int n)
{
    if (n < 1)
        return 0;
 
    int lcm = arr[0];
 
    // To calculate lcm of two numbers
    // multiply them and divide the result
    // by gcd of both the numbers
    for (int i = 1; i < n; i++)
        lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
 
    // Return the LCM of the array elements
    return lcm;
}
 
// Function to return the smaintest perfect cube
// divisible by aint the elements of arr[]
static int minPerfectCube(int arr[], int n)
{
    int minPerfectCube;
 
    // LCM of all the elements of arr[]
    int lcm = lcmOfArray(arr, n);
    minPerfectCube = lcm;
 
    int cnt = 0;
    while (lcm > 1 && lcm % 2 == 0)
    {
        cnt++;
        lcm /= 2;
    }
 
    // If 2 divides lcm cnt number of times
    if (cnt % 3 == 2)
        minPerfectCube *= 2;
    else if (cnt % 3 == 1)
        minPerfectCube *= 4;
 
    int i = 3;
 
    // Check aint the numbers that divide lcm
    while (lcm > 1)
    {
        cnt = 0;
        while (lcm % i == 0)
        {
            cnt++;
            lcm /= i;
        }
 
        if (cnt % 3 == 1)
            minPerfectCube *= i * i;
        else if (cnt % 3 == 2)
            minPerfectCube *= i;
 
        i += 2;
    }
 
    // Return the answer
    return minPerfectCube;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 10, 125, 14, 42, 100 };
    int n = arr.length;
    System.out.println(minPerfectCube(arr, n));
}
}
 
// This code is contributed by
// Surendra_Gangwar

Python3

# Python3 implementation of the approach
 
# Function to return the gcd of two numbers
def gcd(a, b) :
     
    if (b == 0) :
        return a
    else :
        return gcd(b, a % b)
 
# Function to return the lcm of
# all the elements of the array
def lcmOfArray(arr, n) :
     
    if (n < 1) :
        return 0
 
    lcm = arr[0]
 
    # To calculate lcm of two numbers
    # multiply them and divide the result
    # by gcd of both the numbers
    for i in range(n) :
        lcm = (lcm * arr[i]) // gcd(lcm, arr[i]);
 
    # Return the LCM of the array elements
    return lcm
 
# Function to return the smallest perfect cube
# divisible by all the elements of arr[]
def minPerfectCube(arr, n) :
     
    # LCM of all the elements of arr[]
    lcm = lcmOfArray(arr, n)
    minPerfectCube = lcm
 
    cnt = 0
    while (lcm > 1 and lcm % 2 == 0) :
        cnt += 1
        lcm //= 2
     
    # If 2 divides lcm cnt number of times
    if (cnt % 3 == 2) :
        minPerfectCube *= 2
         
    elif (cnt % 3 == 1) :
        minPerfectCube *= 4
 
    i = 3
     
    # Check all the numbers that divide lcm
    while (lcm > 1) :
        cnt = 0
         
        while (lcm % i == 0) :
            cnt += 1
            lcm //= i
         
        if (cnt % 3 == 1) :
            minPerfectCube *= i * i
             
        elif (cnt % 3 == 2) :
            minPerfectCube *= i
 
        i += 2
 
    # Return the answer
    return minPerfectCube
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 10, 125, 14, 42, 100 ]
     
    n = len(arr)
    print(minPerfectCube(arr, n))
     
# This code is contributed by Ryuga

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the gcd of two numbers
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to return the lcm of
// aint the elements of the array
static int lcmOfArray(int []arr, int n)
{
    if (n < 1)
        return 0;
 
    int lcm = arr[0];
 
    // To calculate lcm of two numbers
    // multiply them and divide the result
    // by gcd of both the numbers
    for (int i = 1; i < n; i++)
        lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
 
    // Return the LCM of the array elements
    return lcm;
}
 
// Function to return the smaintest perfect cube
// divisible by aint the elements of arr[]
static int minPerfectCube(int []arr, int n)
{
    int minPerfectCube;
 
    // LCM of all the elements of arr[]
    int lcm = lcmOfArray(arr, n);
    minPerfectCube = lcm;
 
    int cnt = 0;
    while (lcm > 1 && lcm % 2 == 0)
    {
        cnt++;
        lcm /= 2;
    }
 
    // If 2 divides lcm cnt number of times
    if (cnt % 3 == 2)
        minPerfectCube *= 2;
    else if (cnt % 3 == 1)
        minPerfectCube *= 4;
 
    int i = 3;
 
    // Check aint the numbers that divide lcm
    while (lcm > 1)
    {
        cnt = 0;
        while (lcm % i == 0)
        {
            cnt++;
            lcm /= i;
        }
 
        if (cnt % 3 == 1)
            minPerfectCube *= i * i;
        else if (cnt % 3 == 2)
            minPerfectCube *= i;
 
        i += 2;
    }
 
    // Return the answer
    return minPerfectCube;
}
 
// Driver code
public static void Main()
{
    int []arr = { 10, 125, 14, 42, 100 };
    int n = arr.Length;
    Console.WriteLine(minPerfectCube(arr, n));
}
}
 
// This code is contributed by chandan_jnu

PHP

<?php
// PHP implementation of the approach
 
// Function to return the gcd of two numbers
function gcd($a, $b)
{
    if ($b == 0)
        return $a;
    else
        return gcd($b, $a % $b);
}
 
// Function to return the lcm of
// all the elements of the array
function lcmOfArray(&$arr, $n)
{
    if ($n < 1)
        return 0;
 
    $lcm = $arr[0];
 
    // To calculate lcm of two numbers
    // multiply them and divide the result
    // by gcd of both the numbers
    for ($i = 1; $i < $n; $i++)
        $lcm = ($lcm * $arr[$i]) /
            gcd($lcm, $arr[$i]);
 
    // Return the LCM of the array elements
    return $lcm;
}
 
// Function to return the smallest perfect cube
// divisible by all the elements of arr[]
function minPerfectCube(&$arr, $n)
{
     
    // LCM of all the elements of arr[]
    $lcm = lcmOfArray($arr, $n);
    $minPerfectCube = $lcm;
 
    $cnt = 0;
    while ($lcm > 1 && $lcm % 2 == 0)
    {
        $cnt++;
        $lcm /= 2;
    }
 
    // If 2 divides lcm cnt number of times
    if ($cnt % 3 == 2)
        $minPerfectCube *= 2;
    else if ($cnt % 3 == 1)
        $minPerfectCube *= 4;
 
    $i = 3;
 
    // Check all the numbers that divide lcm
    while ($lcm > 1)
    {
        $cnt = 0;
        while ($lcm % $i == 0)
        {
            $cnt++;
            $lcm /= $i;
        }
 
        if ($cnt % 3 == 1)
            $minPerfectCube *= $i * $i;
        else if ($cnt % 3 == 2)
            $minPerfectCube *= $i;
 
        $i += 2;
    }
 
    // Return the answer
    return $minPerfectCube;
}
 
// Driver code
$arr = array(10, 125, 14, 42, 100 );
$n = sizeof($arr);
echo(minPerfectCube($arr, $n));
 
// This code is contributed by Shivi_Aggarwal
?>

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function to return the gcd of two numbers
function gcd(a, b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to return the lcm of
// all the elements of the array
function lcmOfArray(arr, n)
{
    if (n < 1)
        return 0;
 
    let lcm = arr[0];
 
    // To calculate lcm of two numbers
    // multiply them and divide the result
    // by gcd of both the numbers
    for (let i = 1; i < n; i++)
        lcm = parseInt((lcm * arr[i]) / gcd(lcm, arr[i]));
 
    // Return the LCM of the array elements
    return lcm;
}
 
// Function to return the smallest perfect cube
// divisible by all the elements of arr[]
function minPerfectCube(arr, n)
{
    let minPerfectCube;
 
    // LCM of all the elements of arr[]
    let lcm = lcmOfArray(arr, n);
    minPerfectCube = lcm;
 
    let cnt = 0;
    while (lcm > 1 && lcm % 2 == 0) {
        cnt++;
        lcm = parseInt(lcm/2);
    }
 
    // If 2 divides lcm cnt number of times
    if (cnt % 3 == 2)
        minPerfectCube *= 2;
    else if (cnt % 3 == 1)
        minPerfectCube *= 4;
 
    let i = 3;
 
    // Check all the numbers that divide lcm
    while (lcm > 1) {
        cnt = 0;
        while (lcm % i == 0) {
            cnt++;
            lcm = parseInt(lcm/i);
        }
 
        if (cnt % 3 == 1)
            minPerfectCube *= i * i;
        else if (cnt % 3 == 2)
            minPerfectCube *= i;
 
        i += 2;
    }
 
    // Return the answer
    return minPerfectCube;
}
 
// Driver code
let arr = [ 10, 125, 14, 42, 100 ];
let n = arr.length;
document.write(minPerfectCube(arr, n));
 
</script>
Producción: 

9261000

 

Complejidad de tiempo: O(n * log(arr[i])

Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.

Publicación traducida automáticamente

Artículo escrito por Vivek.Pandit y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *