Dada una array de N elementos positivos. La tarea es encontrar un elemento que sea igual a la suma de todos los elementos de la array excepto él mismo.
Ejemplos:
Input: arr[] = {1, 2, 3, 6} Output: 6 6 is the element which is equal to the sum of all remaining elements i.e. 1 + 2+ 3 = 6 Input: arr[] = {2, 2, 2, 2} Output: -1
Enfoque: en primer lugar, encuentre la suma de todos los elementos de una array. Luego itere sobre cada elemento y verifique la condición de que if (a[i] == sum-a[i] ) . Si es verdadero, imprima ese a[i] , de lo contrario, imprima «-1» si no se encuentra dicho elemento.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> #define ll long long int using namespace std; // Function to find the element int findEle(int arr[], int n) { // sum is use to store // sum of all elements // of array ll sum = 0; for (int i = 0; i < n; i++) sum += arr[i]; // iterate over all elements for (int i = 0; i < n; i++) if (arr[i] == sum - arr[i]) return arr[i]; return -1; } // Driver code int main() { int arr[] = { 1, 2, 3, 6 }; int n = sizeof(arr) / sizeof(arr[0]); cout << findEle(arr, n); return 0; }
Java
// Java implementation of the above approach import java.io.*; class GFG { // Function to find the element static int findEle(int arr[], int n) { // sum is use to store // sum of all elements // of array int sum = 0; for (int i = 0; i < n; i++) sum += arr[i]; // iterate over all elements for (int i = 0; i < n; i++) if (arr[i] == sum - arr[i]) return arr[i]; return -1; } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 3, 6 }; int n = arr.length; System.out.print(findEle(arr, n)); } } // This code is contributed by shs..
Python3
# Python 3 implementation of # the above approach # Function to find the element def findEle(arr, n) : # sum is use to store # sum of all elements # of array sum = 0 for i in range(n) : sum += arr[i] # iterate over all elements for i in range(n) : if arr[i] == sum - arr[i] : return arr[i] return -1 # Driver Code if __name__ == "__main__" : arr = [1, 2, 3, 6 ] n = len(arr) print(findEle(arr, n)) # This code is contributed by Ryuga
C#
// C# implementation of the // above approach using System; class GFG { // Function to find the element static int findEle(int []arr, int n) { // sum is use to store // sum of all elements // of array int sum = 0; for (int i = 0; i < n; i++) sum += arr[i]; // iterate over all elements for (int i = 0; i < n; i++) if (arr[i] == sum - arr[i]) return arr[i]; return -1; } // Driver code static public void Main (String []args) { int []arr = { 1, 2, 3, 6 }; int n = arr.Length; Console.WriteLine(findEle(arr, n)); } } // This code is contributed // by Arnab Kundu
PHP
<?php // PHP implementation of the above approach // Function to find the element function findEle($arr, $n) { // sum is use to store // sum of all elements // of array $sum = 0; for ($i = 0; $i < $n; $i++) $sum += $arr[$i]; // iterate over all elements for ($i = 0; $i < $n; $i++) if ($arr[$i] == $sum - $arr[$i]) return $arr[$i]; return -1; } // Driver code $arr= array(1, 2, 3, 6 ); $n = sizeof($arr); echo findEle($arr, $n); // This code is contributed // by Akanksha Rai ?>
Javascript
<script> // javascript implementation of the above approach // Function to find the element function findEle(arr, n) { // sum is use to store // sum of all elements // of array var sum = 0; for (var i = 0; i < n; i++) sum += arr[i]; // iterate over all elements for (var i = 0; i < n; i++) if (arr[i] == sum - arr[i]) return arr[i]; return -1; } // Driver code var arr = [1, 2, 3, 6]; var n = arr.length; document.write(findEle(arr, n)); // This code is contributed by ipg016107. </script>
6
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Nota: el problema anterior se puede resolver con el concepto utilizado en Comprobar si la array tiene un elemento que es igual a la suma de todos los elementos restantes.
Publicación traducida automáticamente
Artículo escrito por sahilshelangia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA