Dada una array arr[] y un entero X , la tarea es contar los elementos de la array de modo que existan un elemento o en la array.
Ejemplos:
Entrada: arr[] = {3, 4, 2, 5}, X = 2
Salida: 4
Explicación:
En el ejemplo anterior, hay 4 de esos números –
Para el elemento 3: los números posibles son 1, 5, mientras que 5 está presente en la array
Para el elemento 4: los números posibles son 2, 6, mientras que 2 está presente en la array
Para el elemento 2: los números posibles son 0, 4, mientras que 4 está presente en la array
Para el elemento 5: los números posibles son 3, 7, mientras que 3 está presente en la array
Por lo tanto, Conteo total = 4
Entrada: arr[] = {2, 2, 4, 5, 6}, X = 3
Salida: 3
Explicación:
En el ejemplo anterior, hay 3 números { 2, 2, 5}
Enfoque: La idea es usar hash-map para comprobar que un elemento está presente en el hash-map o no en el tiempo O(1). Luego, itere sobre los elementos de la array y para cada elemento verifique que o esté presente en la array. En caso afirmativo, aumente el recuento de dichos elementos en 1.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to count of // elements such that its sum/difference // with X also exists in the Array #include <bits/stdc++.h> using namespace std; // Function to find the count of // elements in the array such that // element at the difference at X // is present in the array void findAns(int arr[], int n, int x) { int ans; unordered_set<int> s; // Loop to insert the elements // of the array into the set for (int i = 0; i < n; i++) s.insert(arr[i]); ans = 0; // Loop to iterate over the array for (int i = 0; i < n; i++) { // if any of the elements are there // then increase the count variable if (s.find(arr[i] + x) != s.end() || s.find(arr[i] - x) != s.end()) ans++; } cout << ans; return; } // Driver Code int main() { int arr[] = { 2, 2, 4, 5, 6 }; int n = sizeof(arr) / sizeof(int); int x = 3; findAns(arr, n, x); return 0; }
Java
// Java implementation to count of // elements such that its sum/difference // with X also exists in the Array import java.util.*; class GFG{ // Function to find the count of // elements in the array such that // element at the difference at X // is present in the array static void findAns(int arr[], int n, int x) { int ans; HashSet<Integer> s = new HashSet<Integer>(); // Loop to insert the elements // of the array into the set for (int i = 0; i < n; i++) s.add(arr[i]); ans = 0; // Loop to iterate over the array for (int i = 0; i < n; i++) { // if any of the elements are there // then increase the count variable if (s.contains(arr[i] + x) || s.contains(arr[i] - x)) ans++; } System.out.print(ans); return; } // Driver Code public static void main(String[] args) { int arr[] = { 2, 2, 4, 5, 6 }; int n = arr.length; int x = 3; findAns(arr, n, x); } } // This code is contributed by Rajput-Ji
Python3
# Python3 implementation to count of # elements such that its sum/difference # with X also exists in the array # Function to find the count of # elements in the array such that # element at the difference at X # is present in the array def findAns(arr, n, x): s = set() # Loop to insert the elements # of the array into the set for i in range(n): s.add(arr[i]) ans = 0 # Loop to iterate over the array for i in range(n): # If any of the elements are there # then increase the count variable if arr[i] + x in s or arr[i] - x in s: ans = ans + 1 print(ans) # Driver Code arr = [ 2, 2, 4, 5, 6 ] n = len(arr) x = 3 # Function call findAns(arr, n, x) # This code is contributed by ishayadav181
C#
// C# implementation to count of // elements such that its sum/difference // with X also exists in the Array using System; using System.Collections.Generic; class GFG{ // Function to find the count of // elements in the array such that // element at the difference at X // is present in the array static void findAns(int[] arr, int n, int x) { int ans; HashSet<int> s = new HashSet<int>(); // Loop to insert the elements // of the array into the set for(int i = 0; i < n; i++) s.Add(arr[i]); ans = 0; // Loop to iterate over the array for(int i = 0; i < n; i++) { // if any of the elements are there // then increase the count variable if (s.Contains(arr[i] + x) || s.Contains(arr[i] - x)) ans++; } Console.Write(ans); return; } // Driver Code public static void Main(String[] args) { int[] arr = { 2, 2, 4, 5, 6 }; int n = arr.Length; int x = 3; findAns(arr, n, x); } } // This code is contributed by ShubhamCoder
Javascript
<script> // Javascript implementation to count of // elements such that its sum/difference // with X also exists in the Array // Function to find the count of // elements in the array such that // element at the difference at X // is present in the array function findAns(arr, n, x) { let ans; let s = new Set(); // Loop to insert the elements // of the array leto the set for (let i = 0; i < n; i++) s.add(arr[i]); ans = 0; // Loop to iterate over the array for (let i = 0; i < n; i++) { // if any of the elements are there // then increase the count variable if (s.has(arr[i] + x) || s.has(arr[i] - x)) ans++; } document.write(ans); return; } // Driver code let arr = [ 2, 2, 4, 5, 6 ]; let n = arr.length; let x = 3; findAns(arr, n, x); // This code is contributed by code_hunt. </script>
3
Análisis de rendimiento:
- Complejidad de tiempo: O(N)
- Complejidad del espacio auxiliar: O(N)