Dada una array arr[] de tamaño N , cada entero del rango [1, N] aparece exactamente una vez excepto A que aparece dos veces y B que falta . La tarea es encontrar los números A y B.
Ejemplos:
Entrada: arr[] = {1, 2, 2, 3, 4}
Salida:
A = 2
B = 5
Entrada: arr[] = {5, 3, 4, 1, 1}
Salida:
A = 1
B = 2
Enfoque: A partir de la suma de los primeros N números naturales,
SumN = 1 + 2 + 3 + … + N = (N * (N + 1)) / 2
Y, sea la suma de todos los elementos de la array Sum . Ahora,
SumaN = Suma – A + B
A – B = Suma – SumaN …(ecuación 1)
Y de la suma de los cuadrados de los primeros N números naturales,
SumSqN = 1 2 + 2 2 + 3 2 + … + N 2 = (N * (N + 1) * (2 * n + 1)) / 6
Y, sea SumSq la suma de los cuadrados de todos los elementos del arreglo . Ahora,
SumSq = SumSqN + A 2 – B 2
SumSq – SumSqN = (A + B) * (A – B) …(ecuación 2)
Ponga el valor de (A – B) de la ecuación 1 en la ecuación 2,
SumSq – SumSqN = (A + B) * (Sum – SumN)
A + B = (SumSq – SumSqN) / (Sum – SumN) …(ecuación 3)
Resolver la ecuación 1 y la ecuación 3 dará,
B = (((SumSq – SumSqN) / (Sum – SumN)) + SumN – Sum) / 2
Y, A = Sum – SumN + B
A continuación se muestra la implementación del enfoque anterior:
C++
//C++ implementation of the approach #include <cmath> #include<bits/stdc++.h> #include <iostream> using namespace std; // Function to print the required numbers void findNumbers(int arr[], int n) { // Sum of first n natural numbers int sumN = (n * (n + 1)) / 2; // Sum of squares of first n natural numbers int sumSqN = (n * (n + 1) * (2 * n + 1)) / 6; // To store the sum and sum of squares // of the array elements int sum = 0, sumSq = 0, i; for (i = 0; i < n; i++) { sum += arr[i]; sumSq = sumSq + (pow(arr[i], 2)); } int B = (((sumSq - sumSqN) / (sum - sumN)) + sumN - sum) / 2; int A = sum - sumN + B; cout << "A = " ; cout << A << endl; cout << "B = " ; cout << B << endl; } // Driver code int main() { int arr[] = { 1, 2, 2, 3, 4 }; int n = sizeof(arr)/sizeof(arr[0]); findNumbers(arr, n); return 0; }
Java
// Java implementation of the approach public class GFG { // Function to print the required numbers static void findNumbers(int arr[], int n) { // Sum of first n natural numbers int sumN = (n * (n + 1)) / 2; // Sum of squares of first n natural numbers int sumSqN = (n * (n + 1) * (2 * n + 1)) / 6; // To store the sum and sum of squares // of the array elements int sum = 0, sumSq = 0, i; for (i = 0; i < n; i++) { sum += arr[i]; sumSq += Math.pow(arr[i], 2); } int B = (((sumSq - sumSqN) / (sum - sumN)) + sumN - sum) / 2; int A = sum - sumN + B; System.out.println("A = " + A + "\nB = " + B); } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, 2, 3, 4 }; int n = arr.length; findNumbers(arr, n); } }
Python3
# Python3 implementation of the approach import math # Function to print the required numbers def findNumbers(arr, n): # Sum of first n natural numbers sumN = (n * (n + 1)) / 2; # Sum of squares of first n natural numbers sumSqN = (n * (n + 1) * (2 * n + 1)) / 6; # To store the sum and sum of squares # of the array elements sum = 0; sumSq = 0; for i in range(0,n): sum = sum + arr[i]; sumSq = sumSq + (math.pow(arr[i], 2)); B = (((sumSq - sumSqN) / (sum - sumN)) + sumN - sum) / 2; A = sum - sumN + B; print("A = ",int(A)) ; print("B = ",int(B)); # Driver code arr = [ 1, 2, 2, 3, 4 ]; n = len(arr); findNumbers(arr, n); #This code is contributed by Shivi_Aggarwal
C#
// C# implementation of the approach using System; public class GFG { // Function to print the required numbers static void findNumbers(int []arr, int n) { // Sum of first n natural numbers int sumN = (n * (n + 1)) / 2; // Sum of squares of first n natural numbers int sumSqN = (n * (n + 1) * (2 * n + 1)) / 6; // To store the sum and sum of squares // of the array elements int sum = 0, sumSq = 0, i; for (i = 0; i < n; i++) { sum += arr[i]; sumSq += (int)Math.Pow(arr[i], 2); } int B = (((sumSq - sumSqN) / (sum - sumN)) + sumN - sum) / 2; int A = sum - sumN + B; Console.WriteLine("A = " + A + "\nB = " + B); } // Driver code public static void Main() { int []arr = { 1, 2, 2, 3, 4 }; int n = arr.Length; findNumbers(arr, n); } } // This code is contributed by PrinciRaj1992
PHP
<?php // PHP implementation of the approach // Function to print the required numbers function findNumbers($arr, $n) { // Sum of first n natural numbers $sumN = ($n * ($n + 1)) / 2; // Sum of squares of first n // natural numbers $sumSqN = ($n * ($n + 1) * (2 * $n + 1)) / 6; // To store the sum and sum of // squares of the array elements $sum = 0 ; $sumSq = 0 ; for ($i = 0; $i < $n; $i++) { $sum += $arr[$i]; $sumSq += pow($arr[$i], 2); } $B = ((($sumSq - $sumSqN) / ($sum - $sumN)) + $sumN - $sum) / 2; $A = $sum - $sumN + $B; echo "A = ", $A, "\nB = ", $B; } // Driver code $arr = array( 1, 2, 2, 3, 4 ); $n = sizeof($arr) ; findNumbers($arr, $n); // This code is contributed by Ryuga ?>
Javascript
<script> // Javascript implementation of the approach // Function to print the required numbers function findNumbers(arr, n) { // Sum of first n natural numbers sumN = (n * (n + 1)) / 2; // Sum of squares of first n // natural numbers sumSqN = (n * (n + 1) * (2 * n + 1)) / 6; // To store the sum and sum of // squares of the array elements let sum = 0 ; let sumSq = 0 ; for (let i = 0;i < n; i++) { sum += arr[i]; sumSq += Math.pow(arr[i], 2); } B = (((sumSq - sumSqN) / (sum - sumN)) + sumN - sum) / 2; A = sum - sumN + B; document.write( "A = "+ A, "<br>B = ", B); } // Driver code let arr = [ 1, 2, 2, 3, 4 ]; n = arr.length ; findNumbers(arr, n); // This code is contributed // by bobby </script>
A = 2 B = 5
Complejidad Temporal: O(N), ya que el ciclo va de 0 a (n – 1).
Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.
Publicación traducida automáticamente
Artículo escrito por AmishGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA