Sum of elements till the smallest index such that there are no even numbers to its right Last Updated : 06 Nov, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array arr[] of N integers. The task is to find the sum of elements to 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 = 16Input: 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 to 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 rightmost even element while (i >= 0 && arr[i] % 2 == 1) i--; // To store the required 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; } 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 rightmost even element while (i >= 0 && arr[i] % 2 == 1) i--; // To store the required 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 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 rightmost even element while (i >= 0 and arr[i] % 2 == 1): i -= 1; # To store the required 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 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 rightmost even element while (i >= 0 && arr[i] % 2 == 1) i--; // To store the required 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. JavaScript <script> // Javascript implementation of the approach // Function to return the required sum function smallestIndexsum(arr, n) { // Starting from the last index let i = n - 1; // Skip all odd elements and find the // index of the rightmost even element while (i >= 0 && arr[i] % 2 == 1) i--; // To store the required sum let sum = 0; for (let j = 0; j <= i; j++) sum += arr[j]; return sum; } let arr = [ 2, 3, 5, 6, 3, 3 ]; let n = arr.length; document.write(smallestIndexsum(arr, n)); </script> Output: 16 Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Smallest number greater or equals to N such that it has no odd positioned bit set A Akshita207 Follow Improve Article Tags : DSA Arrays Constructive Algorithms Practice Tags : Arrays Similar Reads Number of permutations such that sum of elements at odd index and even index are equal Given N numbers, find the number of permutations in which the sum of elements at odd index and sum of elements at even index are equal. Examples: Input: 1 2 3 Output: 2 The permutations are: 1 3 2 sum at odd index = 1+2 = 3, sum at even index = 3 2 3 1 sum at odd index = 2+1 = 3, sum at even index = 8 min read Delete odd and even numbers at alternate step such that sum of remaining elements is minimized Given an array arr[] of N elements. At any step, we can delete a number of a different parity from the just previous step, i.e., if, at the previous step, an odd number was deleted then delete an even number in the current step or vice versa. It is allowed to start by deleting any number. Deletion i 7 min read Reverse a subarray of the given array to minimize the sum of elements at even position Given an array arr[] of positive integers. The task is to reverse a subarray to minimize the sum of elements at even places and print the minimum sum. Note: Perform the move only one time. Subarray might not be reversed. Example: Input: arr[] = {1, 2, 3, 4, 5} Output: 7 Explanation: Sum of elements 15 min read Sum of elements in range L-R where first half and second half is filled with odd and even numbers Given a number N, create an array such the first half of the array is filled with odd numbers till N, and the second half of the array is filled with even numbers. Also given are L and R indices, the task is to print the sum of elements in the array in the range [L, R]. Examples: Input: N = 12, L = 15+ min read Smallest number greater or equals to N such that it has no odd positioned bit set Given an integer N, the task is to find the smallest integer X such that it has no odd position set and X ? N. Note: The positioning of bits is assumed from the right side and the first bit is assumed to be the 0th bit. Examples: Input: N = 9 Output: 16 16's binary representation is 10000, which has 12 min read Minimum elements to be removed such that sum of adjacent elements is always even Given an array of N integers. The task is to eliminate the minimum number of elements such that in the resulting array the sum of any two adjacent values is even. Examples: Input : arr[] = {1, 2, 3} Output : 1 Remove 2 from the array. Input : arr[] = {1, 3, 5, 4, 2} Output : 2 Remove 4 and 2. Approa 4 min read Like