Comprueba si la suma de cuadrados perfectos en una array es divisible por x

Dada una array arr[] y un entero x , la tarea es verificar si la suma de todos los cuadrados perfectos de la array es divisible por x o no. Si es divisible, imprima ; de lo contrario, imprima No.
Ejemplos: 
 

Entrada: arr[] = {2, 3, 4, 6, 9, 10}, x = 13 
Salida: Sí 
4 y 9 son los únicos cuadrados perfectos de la array 
suma = 4 + 9 = 13 (que es divisible por 13 )
Entrada: arr[] = {2, 4, 25, 49, 3, 8}, x = 9 
Salida: No 
 

Enfoque: Ejecute un ciclo de i a n – 1 y verifique si arr[i] es un cuadrado perfecto o no. Si arr[i] es un cuadrado perfecto, actualice sum = sum + arr[i] . Si al final la suma % x = 0 , imprima ; de lo contrario, imprima No. Para comprobar si un elemento es un cuadrado perfecto o no, siga los siguientes pasos: 
 

Sea num un elemento entero 
float sq = sqrt(x) 
si floor(sq) = ceil(sq) entonces num es un cuadrado perfecto, de lo contrario no. 
 

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that returns true if the sum of all the
// perfect squares of the given array are divisible by x
bool check(int arr[], int x, int n)
{
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        double x = sqrt(arr[i]);
 
        // If arr[i] is a perfect square
        if (floor(x) == ceil(x)) {
            sum += arr[i];
        }
    }
 
    if (sum % x == 0)
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 3, 4, 9, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 13;
 
    if (check(arr, x, n)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

Java

// Java implementation of the approach
public class GFG{
 
    // Function that returns true if the the sum of all the
    // perfect squares of the given array is divisible by x
    static boolean check(int arr[], int x, int n)
    {
        long sum = 0;
        for (int i = 0; i < n; i++) {
            double y = Math.sqrt(arr[i]);
     
            // If arr[i] is a perfect square
            if (Math.floor(y) == Math.ceil(y)) {
                sum += arr[i];
            }
        }
     
        if (sum % x == 0)
            return true;
        else
            return false;
    }
 
 
 
    // Driver Code
    public static void main(String []args){
        int arr[] = { 2, 3, 4, 9, 10 };
        int n = arr.length ;
        int x = 13;
 
        if (check(arr, x, n)) {
            System.out.println("Yes");
        }
        else {
           System.out.println("No");
        }
    }
    // This code is contributed by Ryuga
}

Python3

# Python3 implementation of the approach
import math
 
# Function that returns true if  the sum of all the
# perfect squares of the given array is divisible by x
def check (a, y):
    sum = 0
    for i in range(len(a)):
         
        x = math.sqrt(a[i])
 
        # If a[i] is a perfect square
        if (math.floor(x) == math.ceil(x)):
            sum = sum + a[i]
     
    if (sum % y == 0):
        return True
    else:
        return False
         
 
# Driver code
a = [2, 3, 4, 9, 10]
x = 13
 
if check(a, x) :
    print("Yes")
else:
    print("No")

C#

// C# implementation of the approach
 
using System;
public class GFG{
  
    // Function that returns true if  the sum of all the
    // perfect squares of the given array is divisible by x
    static bool check(int[] arr, int x, int n)
    {
        long sum = 0;
        for (int i = 0; i < n; i++) {
            double y = Math.Sqrt(arr[i]);
      
            // If arr[i] is a perfect square
            if (Math.Floor(y) == Math.Ceiling(y)) {
                sum += arr[i];
            }
        }
      
        if (sum % x == 0)
            return true;
        else
            return false;
    }
  
  
  
    // Driver Code
    public static void Main(){
        int[] arr = { 2, 3, 4, 9, 10 };
        int n = arr.Length ;
        int x = 13;
  
        if (check(arr, x, n)) {
            Console.Write("Yes");
        }
        else {
           Console.Write("No");
        }
    }   
}

PHP

<?php
// PHP implementation of the approach
 
// Function that returns true if the
// sum of all the perfect squares of
// the given array is divisible by x
function check($arr, $x, $n)
{
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
    {
        $x = sqrt($arr[$i]);
 
        // If arr[i] is a perfect square
        if (floor($x) == ceil($x))
        {
            $sum += $arr[$i];
        }
    }
 
    if (($sum % $x) == 0)
        return true;
    else
        return false;
}
 
// Driver code
$arr = array( 2, 3, 4, 9, 10 );
$n = sizeof($arr);
$x = 13;
 
if (!check($arr, $x, $n))
{
    echo "Yes";
}
else
{
    echo "No";
}
 
// This code is contributed by Sachin
?>

Javascript

<script>
 
// Javascript implementation of the approach
     
    // Function that returns true if the sum of all the
    // perfect squares of the given array is divisible by x
    function check(arr,x,n)
    {
        let sum = 0;
        for (let i = 0; i < n; i++) {
            let y = Math.sqrt(arr[i]);
       
            // If arr[i] is a perfect square
            if (Math.floor(y) == Math.ceil(y)) {
                sum += arr[i];
            }
        }
       
        if (sum % x == 0)
            return true;
        else
            return false;
    }
    // Driver Code
     
    let arr=[ 2, 3, 4, 9, 10];
    let n = arr.length ;
    let x = 13;
    if (check(arr, x, n)) {
        document.write("Yes");
    }
    else {
       document.write("No");
    }
         
    // This code is contributed by unknown2108
     
</script>
Producción: 

Yes

 

Complejidad de tiempo: O (nlogn)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por mohit kumar 29 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 *