Dada una array Arr[] de N de enteros positivos. La tarea es encontrar posiciones de todos los elementos que sean iguales a la suma de todos los elementos precedentes. Si no existe tal elemento, imprima -1.
Ejemplos:
Entrada: Arr[] = {1, 2, 3, 6, 3, 15, 5}
Salida: 3 4 6
Aquí, el elemento en el índice «3», es decir, 3 es igual a la suma de los elementos anteriores (1 + 2) .
De manera similar, en el índice 4, 6 = 1+2+3 (suma de todos los elementos anteriores).
Y elemento en el índice 6, es decir, 15 = 1 + 2 + 3 + 6 + 3.
Entrada: Arr[] = {7, 5, 17, 25}
Salida: -1
Enfoque:
Mientras recorre la array Arr[] , mantenga una variable de suma que almacene la suma de elementos hasta i – 1 . Compare la suma con el elemento actual Arr[i] . Si es igual, inserte el índice de este elemento en el vector de respuesta.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation #include <bits/stdc++.h> using namespace std; // function to return valid indexes vector<int> find_idx(int ar[], int n) { // Vector to store the answer vector<int> answer; // Initial sum would always // be first element int sum = ar[0]; for (int i = 1; i < n; i++) { // Check if sum till now // is equal to current element if (sum == ar[i]) { answer.push_back(i + 1); } // Updating the sum by // adding the current // element in each // iteration. sum += ar[i]; } return answer; } // Driver code int main() { int ar[] = { 1, 2, 3, 6, 3, 15, 5 }; int n = sizeof(ar) / sizeof(int); vector<int> ans = find_idx(ar, n); if (ans.size() != 0) { for (int i : ans) { cout << i << " "; } } else { cout << "-1"; } cout << endl; return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // function to return valid indexes static Vector<Integer> find_idx(int ar[], int n) { // Vector to store the answer Vector<Integer> answer = new Vector<Integer>(); // Initial sum would always // be first element int sum = ar[0]; for (int i = 1; i < n; i++) { // Check if sum till now // is equal to current element if (sum == ar[i]) { answer.add(i + 1); } // Updating the sum by adding the // current element in each iteration. sum += ar[i]; } return answer; } // Driver code public static void main(String[] args) { int ar[] = { 1, 2, 3, 6, 3, 15, 5 }; int n = ar.length; Vector<Integer> ans = find_idx(ar, n); if (ans.size() != 0) { for (int i : ans) { System.out.print(i + " "); } } else { System.out.println("-1"); } } } // This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the above approach # function to return valid indexes def find_idx(ar, n) : # Vector to store the answer answer = []; # Initial sum would always # be first element sum = ar[0]; for i in range(1, n) : # Check if sum till now # is equal to current element if (sum == ar[i]) : answer.append(i + 1); # Updating the sum by # adding the current # element in each # iteration. sum += ar[i]; return answer; # Driver code if __name__ == "__main__" : ar = [ 1, 2, 3, 6, 3, 15, 5 ]; n = len(ar); ans = find_idx(ar, n); if (len(ans) != 0) : for i in ans : print(i, end = " "); else : print("-1"); print(); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { // function to return valid indexes static List<int> find_idx(int []ar, int n) { // Vector to store the answer List<int> answer = new List<int>(); // Initial sum would always // be first element int sum = ar[0]; for (int i = 1; i < n; i++) { // Check if sum till now // is equal to current element if (sum == ar[i]) { answer.Add(i + 1); } // Updating the sum by adding the // current element in each iteration. sum += ar[i]; } return answer; } // Driver code public static void Main(String[] args) { int []ar = { 1, 2, 3, 6, 3, 15, 5 }; int n = ar.Length; List<int> ans = find_idx(ar, n); if (ans.Count != 0) { foreach (int i in ans) { Console.Write(i + " "); } } else { Console.WriteLine("-1"); } } } // This code is contributed by Princi Singh
Javascript
<script> // Javascript implementation // function to return valid indexes function find_idx(ar, n) { // Vector to store the answer let answer = []; // Initial sum would always // be first element let sum = ar[0]; for (let i = 1; i < n; i++) { // Check if sum till now // is equal to current element if (sum == ar[i]) { answer.push(i + 1); } // Updating the sum by // adding the current // element in each // iteration. sum += ar[i]; } return answer; } // Driver code let ar = [1, 2, 3, 6, 3, 15, 5]; let n = ar.length; let ans = find_idx(ar, n); if (ans.length != 0) { for (let i of ans) { document.write(i + " "); } } else { document.write("-1"); } document.write("<br>"); // This code is contributed by gfgking. </script>
3 4 6
Complejidad temporal: O(n)
Espacio auxiliar: O(n)
Publicación traducida automáticamente
Artículo escrito por namankhare42 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA