# Index of the elements which are equal to the sum of all succeeding elements

Given an array arr[] of N positive integers. The task is to find the index of the elements which are equal to the sum of all succeeding elements. If no such element exists then print -1.

Examples:

Input: arr[] = { 36, 2, 17, 6, 6, 5 }
Output: 0 2
arr = arr + arr + arr + arr + arr
arr = arr + arr + arr

Input: arr[] = {7, 25, 17, 7}
Output: -1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: While traversing the given array arr[] from last index, maintain a sum variable that stores the sum of elements traversed till now. Compare the sum with the current element arr[i]. If it is equal, push the index of this element into the answer vector. If the size of the answer vector in the end is 0 then print -1 else print its content.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the valid indices ` `void` `find_idx(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// Vector to store the indices ` `    ``vector<``int``> answer; ` ` `  `    ``// Initialise sum with 0 ` `    ``int` `sum = 0; ` ` `  `    ``// Starting from the last element ` `    ``for` `(``int` `i = n - 1; i >= 0; i--) { ` ` `  `        ``// If sum till now is equal to ` `        ``// the current element ` `        ``if` `(sum == arr[i]) { ` `            ``answer.push_back(i); ` `        ``} ` ` `  `        ``// Add current element to the sum ` `        ``sum += arr[i]; ` `    ``} ` ` `  `    ``if` `(answer.size() == 0) { ` `        ``cout << ``"-1"``; ` `        ``return``; ` `    ``} ` ` `  `    ``for` `(``int` `i = answer.size() - 1; i >= 0; i--) ` `        ``cout << answer[i] << ``" "``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 36, 2, 17, 6, 6, 5 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``); ` ` `  `    ``find_idx(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to find the valid indices  ` `    ``static` `void` `find_idx(``int` `arr[], ``int` `n)  ` `    ``{  ` `     `  `        ``// Vector to store the indices  ` `        ``Vector answer = ``new` `Vector(); ` `     `  `        ``// Initialise sum with 0  ` `        ``int` `sum = ``0``;  ` `     `  `        ``// Starting from the last element  ` `        ``for` `(``int` `i = n - ``1``; i >= ``0``; i--) ` `        ``{  ` `     `  `            ``// If sum till now is equal to  ` `            ``// the current element  ` `            ``if` `(sum == arr[i])  ` `            ``{  ` `                ``answer.add(i);  ` `            ``}  ` `     `  `            ``// Add current element to the sum  ` `            ``sum += arr[i];  ` `        ``}  ` `     `  `        ``if` `(answer.size() == ``0``) ` `        ``{  ` `            ``System.out.println(``"-1"``);  ` `            ``return``;  ` `        ``}  ` `     `  `        ``for` `(``int` `i = answer.size() - ``1``; i >= ``0``; i--)  ` `            ``System.out.print(answer.get(i) + ``" "``);  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{  ` `        ``int` `arr[] = { ``36``, ``2``, ``17``, ``6``, ``6``, ``5` `};  ` `        ``int` `n = arr.length;  ` `     `  `        ``find_idx(arr, n);  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to find the valid indices  ` `def` `find_idx(arr, n):  ` ` `  `    ``# List to store the indices  ` `    ``answer``=``[]  ` ` `  `    ``# Initialise sum with 0  ` `    ``_sum ``=` `0` ` `  `    ``# Starting from the last element  ` `    ``for` `i ``in` `range``(n ``-` `1``, ``-``1``, ``-``1``): ` ` `  `        ``# If sum till now is equal to  ` `        ``# the current element  ` `        ``if` `(_sum ``=``=` `arr[i]) :  ` `            ``answer.append(i)  ` ` `  `        ``# Add current element to the sum  ` `        ``_sum ``+``=` `arr[i]  ` ` `  `    ``if` `(``len``(answer) ``=``=` `0``) :  ` `        ``print``(``-``1``)  ` `        ``return` ` `  `    ``for` `i ``in` `range``(``len``(answer) ``-` `1``, ``-``1``, ``-``1``):  ` `        ``print``(answer[i], end ``=` `" "``)  ` ` `  `# Driver code  ` `arr ``=` `[ ``36``, ``2``, ``17``, ``6``, ``6``, ``5` `]  ` `n ``=` `len``(arr)  ` ` `  `find_idx(arr, n) ` ` `  `# This code is contributed by ` `# divyamohan123 `

## C#

 `// C# implementation of the approach  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to find the valid indices  ` `    ``static` `void` `find_idx(``int``[] arr, ``int` `n)  ` `    ``{  ` `     `  `        ``// List to store the indices  ` `        ``List<``int``> answer = ``new` `List<``int``>(); ` `     `  `        ``// Initialise sum with 0  ` `        ``int` `sum = 0;  ` `     `  `        ``// Starting from the last element  ` `        ``for` `(``int` `i = n - 1; i >= 0; i--) ` `        ``{  ` `     `  `            ``// If sum till now is equal to  ` `            ``// the current element  ` `            ``if` `(sum == arr[i])  ` `            ``{  ` `                ``answer.Add(i);  ` `            ``}  ` `     `  `            ``// Add current element to the sum  ` `            ``sum += arr[i];  ` `        ``}  ` `     `  `        ``if` `(answer.Count == 0) ` `        ``{  ` `            ``Console.WriteLine(``"-1"``);  ` `            ``return``;  ` `        ``}  ` `     `  `        ``for` `(``int` `i = answer.Count - 1; i >= 0; i--)  ` `            ``Console.Write(answer[i] + ``" "``);  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main (String[] args)  ` `    ``{  ` `        ``int``[] arr = { 36, 2, 17, 6, 6, 5 };  ` `        ``int` `n = arr.Length;  ` `     `  `        ``find_idx(arr, n);  ` `    ``}  ` `} ` ` `  `// This code is contributed by Ashutosh450 `

Output:

```0 2
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.