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.
Ejemplos:
Entrada: arr[] = {1, 2, 3}
Salida: 2
Explicación:
Hay dos elementos que se pueden representar como la
diferencia del cuadrado de dos números:
elemento 1,
elemento 3,
por lo tanto, el recuento de dichos elementos es 2.
Entrada: arr[] = {2, 5, 6}
Salida: 1
Explicación:
Solo hay un elemento de este tipo. Es decir,
elemento 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 =>
N = 6 => No se puede representar como
N = 8 =>
N = 10 => No se puede representar como
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>
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