Dada una array donde cada elemento aparece ‘a’ veces, excepto un elemento que aparece b (a>b) veces. Encuentra el elemento que ocurre b veces.
Ejemplos:
Input : arr[] = [1, 1, 2, 2, 2, 3, 3, 3] a = 3, b = 2 Output : 1
Sume cada número una vez y multiplique la suma por a, obtendremos a por la suma de cada elemento de la array. Guárdelo como a_sum. Reste la suma de toda la array de a_sum y divida el resultado por (ab). El número que obtenemos es el número requerido (que aparece b veces en la array).
C++
// CPP program to find the only element that // appears b times #include <bits/stdc++.h> using namespace std; int appearsbTimes(int arr[], int n, int a, int b) { unordered_set<int> s; int a_sum = 0, sum = 0; for (int i = 0; i < n; i++) { if (s.find(arr[i]) == s.end()) { s.insert(arr[i]); a_sum += arr[i]; } sum += arr[i]; } a_sum = a * a_sum; return ((a_sum - sum) / (a - b)); } int main() { int arr[] = { 1, 1, 2, 2, 2, 3, 3, 3 }; int a = 3, b = 2; int n = sizeof(arr) / sizeof(arr[0]); cout << appearsbTimes(arr, n, a, b); return 0; }
Java
// Java program to find the only element that // appears b times import java.util.*; class GFG { static int appearsbTimes(int arr[], int n, int a, int b) { HashSet<Integer> s = new HashSet<Integer>(); int a_sum = 0, sum = 0; for (int i = 0; i < n; i++) { if (!s.contains(arr[i])) { s.add(arr[i]); a_sum += arr[i]; } sum += arr[i]; } a_sum = a * a_sum; return ((a_sum - sum) / (a - b)); } // Driver Code public static void main(String[] args) { int arr[] = { 1, 1, 2, 2, 2, 3, 3, 3 }; int a = 3, b = 2; int n = arr.length; System.out.println(appearsbTimes(arr, n, a, b)); } } // This code is contributed by 29AjayKumar
Python3
# Python3 program to find the only # element that appears b times def appearsbTimes(arr, n, a, b): s = dict() a_Sum = 0 Sum = 0 for i in range(n): if (arr[i] not in s.keys()): s[arr[i]] = 1 a_Sum += arr[i] Sum += arr[i] a_Sum = a * a_Sum return ((a_Sum - Sum) // (a - b)) # Driver code arr = [1, 1, 2, 2, 2, 3, 3, 3] a, b = 3, 2 n = len(arr) print(appearsbTimes(arr, n, a, b)) # This code is contributed by mohit kumar
C#
// C# program to find the only element that // appears b times using System; using System.Collections.Generic; class GFG { static int appearsbTimes(int []arr, int n, int a, int b) { HashSet<int> s = new HashSet<int>(); int a_sum = 0, sum = 0; for (int i = 0; i < n; i++) { if (!s.Contains(arr[i])) { s.Add(arr[i]); a_sum += arr[i]; } sum += arr[i]; } a_sum = a * a_sum; return ((a_sum - sum) / (a - b)); } // Driver Code public static void Main(String[] args) { int []arr = { 1, 1, 2, 2, 2, 3, 3, 3 }; int a = 3, b = 2; int n = arr.Length; Console.WriteLine(appearsbTimes(arr, n, a, b)); } } // This code is contributed by Princi Singh
Javascript
<script> // Javascript program to find the only element that // appears b times function appearsbTimes(arr, n, a, b) { var s = new Set(); var a_sum = 0, sum = 0; for (var i = 0; i < n; i++) { if (!s.has(arr[i])) { s.add(arr[i]); a_sum += arr[i]; } sum += arr[i]; } a_sum = a * a_sum; return ((a_sum - sum) / (a - b)); } var arr = [1, 1, 2, 2, 2, 3, 3, 3]; var a = 3, b = 2; var n = arr.length; document.write( appearsbTimes(arr, n, a, b)); </script>
1
Complejidad temporal: O(n)
Espacio auxiliar: O(n)
Consulte el artículo a continuación para obtener más enfoques.
Encuentra el único elemento que aparece k veces.
Publicación traducida automáticamente
Artículo escrito por aganjali10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA