📌  相关文章
📜  计算数组中可以表示为两个完美平方之差的元素

📅  最后修改于: 2021-05-17 03:11:34             🧑  作者: Mango

给定一个数组arr [] ,任务是计算该数组中可以表示为两个完美平方数之差形式的元素的数量。 a^2 - b^2
例子:

方法:问题中的关键观察是数字,它可以表示为两个数字的平方之差,从不除以4所得的余数为2。
例如:

因此,遍历数组并计算数组中此类元素的数量。
下面是上述方法的实现:

C++
// C++ implementation to count the
// number of elements which can be
// represented as the difference
// of the two square
 
#include 
 
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):
    # Intialize 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


输出:
2