Cuente elementos en una array que se puede representar como diferencia de dos cuadrados perfectos

Dada una array arr[] , la tarea es contar el número de elementos de la array que se pueden representar como la diferencia de dos números cuadrados perfectos. a^2 - b^2
Ejemplos: 
 

Entrada: arr[] = {1, 2, 3} 
Salida:
Explicación: 
Hay dos elementos que se pueden representar como la 
diferencia del cuadrado de dos números: 
elemento 1,  1^2 - 0^2 = 1
elemento 3,  2^2 - 1^2 = 3
por lo tanto, el recuento de dichos elementos es 2.
Entrada: arr[] = {2, 5, 6} 
Salida:
Explicación: 
Solo hay un elemento de este tipo. Es decir, 
elemento 5,  3^2 - 2^2 = 9 - 4 = 5
por lo tanto, el recuento de dichos elementos es 1. 
 

Enfoque: La observación clave en el problema es que los números que se pueden representar como la diferencia de los cuadrados de dos números nunca producen 2 como el resto cuando se divide por 4. 
Por ejemplo: 
 

N = 4 =>  4^2 - 0^2
N = 6 => No se puede representar como  6 \% 4 = 2
N = 8 =>  3^2 - 1^2
N = 10 => No se puede representar como 10 \% 4 = 2
 

Por lo tanto, itere sobre la array y cuente el número de tales elementos en la array.
A continuación se muestra la implementación del enfoque anterior:

C++

// C++ implementation to count the
// number of elements which can be
// represented as the difference
// of the two square
 
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to count of such elements
// in the array which can be represented
// as the difference of the two squares
int count_num(int arr[], int n)
{
    // Initialize count
    int count = 0;
 
    // Loop to iterate
    // over the array
    for (int i = 0; i < n; i++)
 
        // Condition to check if the
        // number can be represented
        // as the difference of squares
        if ((arr[i] % 4) != 2)
            count++;
 
    cout << count;
    return 0;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    count_num(arr, n);
    return 0;
}

Java

// Java implementation to count the
// number of elements which can be
// represented as the difference
// of the two square
class GFG{
 
// Function to count of such elements
// in the array which can be represented
// as the difference of the two squares
static void count_num(int []arr, int n)
{
     
    // Initialize count
    int count = 0;
     
    // Loop to iterate
    // over the array
    for(int i = 0; i < n; i++)
    {
        
       // Condition to check if the
       // number can be represented
       // as the difference of squares
       if ((arr[i] % 4) != 2)
           count++;
    }
    System.out.println(count);
}
     
// Driver code
public static void main (String[] args)
{
    int arr[] = { 1, 2, 3 };
    int n = arr.length;
     
    count_num(arr, n);
}
}
 
// This code is contributed by AnkitRai01

Python3

# Python3 implementation to count the
# number of elements in the array
# which can be represented as difference
# of the two elements
 
# Function to return the
# Count of required count
# of such elements
def count_num(arr, n):
    # Initialize count
    count = 0
     
    # Loop to iterate over the
    # array of elements
    for i in arr:
         
        # Condition to check if the
        # number can be represented
        # as the difference
        # of two squares
        if ((i % 4) != 2):
            count = count + 1
     
    return count
 
# Driver Code
if __name__ == "__main__":
    arr = [1, 2, 3]
    n = len(arr)
     
    # Function Call
    print(count_num(arr, n))

C#

// C# implementation to count the
// number of elements which can be
// represented as the difference
// of the two square
using System;
class GFG{
 
// Function to count of such elements
// in the array which can be represented
// as the difference of the two squares
static void count_num(int []arr, int n)
{
     
    // Initialize count
    int count = 0;
     
    // Loop to iterate
    // over the array
    for(int i = 0; i < n; i++)
    {
         
        // Condition to check if the
        // number can be represented
        // as the difference of squares
        if ((arr[i] % 4) != 2)
            count++;
    }
    Console.WriteLine(count);
}
     
// Driver code
public static void Main(string[] args)
{
    int []arr = { 1, 2, 3 };
    int n = arr.Length;
     
    count_num(arr, n);
}
}
 
// This code is contributed by shivanisinghss2110

Javascript

<script>
 
    // Javascript implementation to count the
    // number of elements which can be
    // represented as the difference
    // of the two square
     
    // Function to count of such elements
    // in the array which can be represented
    // as the difference of the two squares
    function count_num(arr, n)
    {
        // Initialize count
        let count = 0;
 
        // Loop to iterate
        // over the array
        for (let i = 0; i < n; i++)
 
            // Condition to check if the
            // number can be represented
            // as the difference of squares
            if ((arr[i] % 4) != 2)
                count++;
 
        document.write(count);
        return 0;
    }
 
    let arr = [ 1, 2, 3 ];
    let n = arr.length;
    count_num(arr, n);
 
</script>
Producción: 

2

 

Publicación traducida automáticamente

Artículo escrito por deepanshu_rustagi 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 *