Given an array **arr[]** of **N** integers. The task is to find the sum of elements till the smallest index such that there are no even elements to the right of the index. **Note** that the array will have at least one even element.

**Examples:**

Input:arr[] = {2, 3, 5, 6, 3, 3}

Output:16

2 + 3 + 5 + 6 = 16

Input:arr[] = {3, 4}

Output:7

3 + 4 = 7

**Approach:** Find the index of the rightmost even element from the array and return the sum of all the elements starting from index 0 till the found index.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the required sum ` `int` `smallestIndexsum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Starting from the last index ` ` ` `int` `i = n - 1; ` ` ` ` ` `// Skip all odd elements and find the ` ` ` `// index of the righmost even element ` ` ` `while` `(i >= 0 && arr[i] % 2 == 1) ` ` ` `i--; ` ` ` ` ` `// To store the requried sum ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `j = 0; j <= i; j++) ` ` ` `sum += arr[j]; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 2, 3, 5, 6, 3, 3 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << smallestIndexsum(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the required sum ` `static` `int` `smallestIndexsum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Starting from the last index ` ` ` `int` `i = n - ` `1` `; ` ` ` ` ` `// Skip all odd elements and find the ` ` ` `// index of the righmost even element ` ` ` `while` `(i >= ` `0` `&& arr[i] % ` `2` `== ` `1` `) ` ` ` `i--; ` ` ` ` ` `// To store the requried sum ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(` `int` `j = ` `0` `; j <= i; j++) ` ` ` `sum += arr[j]; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `3` `, ` `5` `, ` `6` `, ` `3` `, ` `3` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(smallestIndexsum(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the required sum ` `def` `smallestIndexsum(arr, n): ` ` ` ` ` `# Starting from the last index ` ` ` `i ` `=` `n ` `-` `1` `; ` ` ` ` ` `# Skip all odd elements and find the ` ` ` `# index of the righmost even element ` ` ` `while` `(i >` `=` `0` `and` `arr[i] ` `%` `2` `=` `=` `1` `): ` ` ` `i ` `-` `=` `1` `; ` ` ` ` ` `# To store the requried sum ` ` ` `sum` `=` `0` `; ` ` ` `for` `j ` `in` `range` `(` `0` `, i ` `+` `1` `): ` ` ` `sum` `+` `=` `arr[j]; ` ` ` ` ` `return` `sum` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `arr ` `=` `[ ` `2` `, ` `3` `, ` `5` `, ` `6` `, ` `3` `, ` `3` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `print` `(smallestIndexsum(arr, n)); ` ` ` `# This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the required sum ` `static` `int` `smallestIndexsum(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` ` ` `// Starting from the last index ` ` ` `int` `i = n - 1; ` ` ` ` ` `// Skip all odd elements and find the ` ` ` `// index of the righmost even element ` ` ` `while` `(i >= 0 && arr[i] % 2 == 1) ` ` ` `i--; ` ` ` ` ` `// To store the requried sum ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `j = 0; j <= i; j++) ` ` ` `sum += arr[j]; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `[]arr = { 2, 3, 5, 6, 3, 3 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.Write(smallestIndexsum(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

**Output:**

16

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:

- Smallest index such that there are no 0 or 1 to its right
- Queries for number of distinct elements from a given index till last index in an array
- Even numbers at even index and odd numbers at odd index
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- 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
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Number of permutations such that sum of elements at odd index and even index are equal
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Check if every index i has an index j such that sum of elements in both directions are equal
- 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
- Count elements such that there are exactly X elements with values greater than or equal to X
- Count quadruplets(A, B, C, D) till N such that sum of square of A and B is equal to that of C and D
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Replace every element of array with sum of elements on its right side
- Rearrange array such that all even-indexed elements in the Array is even
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array

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.