Dada una array arr[] , la tarea es encontrar el número de pares (arr[i], arr[j]) en la array tal que arr[i] + arr[j] = arr[i] * arr[j ]
Ejemplos:
Entrada: arr[] = {2, 2, 3, 4, 6}
Salida: 1
(2, 2) es el único par posible como (2 + 2) = (2 * 2) = 4.
Entrada: arr[] = {1, 2, 3, 4, 5}
Salida: 0
Enfoque: Los únicos pares posibles de enteros que satisfarán las condiciones dadas son (0, 0) y (2, 2) . Entonces, la tarea ahora es contar el número de 0 y 2 en la array y almacenarlos en cnt0 y cnt2 respectivamente y luego el conteo requerido será (cnt0 * (cnt0 – 1)) / 2 + (cnt2 * (cnt2 – 1) )) / 2 .
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 to return the count // of the required pairs int sumEqualProduct(int a[], int n) { int zero = 0, two = 0; // Find the count of 0s // and 2s in the array for (int i = 0; i < n; i++) { if (a[i] == 0) { zero++; } if (a[i] == 2) { two++; } } // Find the count of required pairs int cnt = (zero * (zero - 1)) / 2 + (two * (two - 1)) / 2; // Return the count return cnt; } // Driver code int main() { int a[] = { 2, 2, 3, 4, 2, 6 }; int n = sizeof(a) / sizeof(a[0]); cout << sumEqualProduct(a, n); return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the count // of the required pairs static int sumEqualProduct(int a[], int n) { int zero = 0, two = 0; // Find the count of 0s // and 2s in the array for (int i = 0; i < n; i++) { if (a[i] == 0) { zero++; } if (a[i] == 2) { two++; } } // Find the count of required pairs int cnt = (zero * (zero - 1)) / 2 + (two * (two - 1)) / 2; // Return the count return cnt; } // Driver code public static void main(String[] args) { int a[] = { 2, 2, 3, 4, 2, 6 }; int n = a.length; System.out.print(sumEqualProduct(a, n)); } } // This code is contributed by Rajput-Ji
Python3
# Python 3 implementation of the approach # Function to return the count # of the required pairs def sumEqualProduct(a, n): zero = 0 two = 0 # Find the count of 0s # and 2s in the array for i in range(n): if a[i] == 0: zero += 1 if a[i] == 2: two += 1 # Find the count of required pairs cnt = (zero * (zero - 1)) // 2 + \ (two * (two - 1)) // 2 # Return the count return cnt # Driver code a = [ 2, 2, 3, 4, 2, 6 ] n = len(a) print(sumEqualProduct(a, n)) # This code is contributed by Ankit kumar
C#
// C# implementation of the approach using System; class GFG { // Function to return the count // of the required pairs static int sumEqualProduct(int []a, int n) { int zero = 0, two = 0; // Find the count of 0s // and 2s in the array for (int i = 0; i < n; i++) { if (a[i] == 0) { zero++; } if (a[i] == 2) { two++; } } // Find the count of required pairs int cnt = (zero * (zero - 1)) / 2 + (two * (two - 1)) / 2; // Return the count return cnt; } // Driver code public static void Main(String[] args) { int []a = { 2, 2, 3, 4, 2, 6 }; int n = a.Length; Console.Write(sumEqualProduct(a, n)); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript implementation of the approach // Function to return the count // of the required pairs function sumEqualProduct(a, n) { var zero = 0, two = 0; // Find the count of 0s // and 2s in the array for (var i = 0; i < n; i++) { if (a[i] == 0) { zero++; } if (a[i] == 2) { two++; } } // Find the count of required pairs var cnt = (zero * (zero - 1)) / 2 + (two * (two - 1)) / 2; // Return the count return cnt; } // Driver code var a = [2, 2, 3, 4, 2, 6]; var n = a.length; document.write( sumEqualProduct(a, n)); // This code is contributed by importantly. </script>
3
Complejidad de tiempo : O(N)
Espacio Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por AmanGupta65 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA