Given an array Arr[] of N of positive integers. The task is to find positions of all the elements which are equal to the sum of all preceding elements. If no such element exists print -1.
Examples:
Input : Arr[] = {1, 2, 3, 6, 3, 15, 5}
Output :3 4 6Here, the element at index “3” i.e. 3 is equal to the sum of preceding elements (1 + 2).
Similarly, at index 4, 6 = 1+2+3 (sum of all preceding elements).
And element at index 6 i.e. 15 = 1 + 2 + 3 + 6 + 3.Input: Arr[] = {7, 5, 17, 25}
Output: -1
Approach:
While traversing the array Arr[], maintain a sum variable that store the sum of elements till i – 1. Compare the sum with current element Arr[i]. If it is equal, push the index of this element into the answer vector.
Below is the implementation of the above approach:
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 |
3 4 6
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of Array elements to be divided by 2 to make at least K elements equal
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Count elements such that there are exactly X elements with values greater than or equal to X
- Index of the elements which are equal to the sum of all succeeding elements
- Count of elements which are equal to the XOR of the next two elements
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Count of elements which are not at the correct position
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Reverse a subarray of the given array to minimize the sum of elements at even position
- Shuffle the position of each Array element by swapping adjacent elements
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Maximum sum of two elements whose digit sum is equal
- Check if Sum and XOR of all elements of array is equal
- XOR of all elements of array with set bits equal to K
- Element equal to the sum of all the remaining elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

