VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sum-even-odd-elements-array/

⇱ Program to print Sum of even and odd elements in an array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to print Sum of even and odd elements in an array

Last Updated : 23 Jul, 2025

Prerequisite - Array Basics 
Given an array, write a program to find the sum of values of even and odd index positions separately.

Examples: 

Input : arr[] = {1, 2, 3, 4, 5, 6}
Output :Even index positions sum 9
 Odd index positions sum 12
Explanation: Here, n = 6 so there will be 3 even 
index positions and 3 odd index positions in an array
Even = 1 + 3 + 5 = 9
Odd = 2 + 4 + 6 = 12

Input : arr[] = {10, 20, 30, 40, 50, 60, 70}
Output : Even index positions sum 160
 Odd index positions sum 120
Explanation: Here, n = 7 so there will be 3 odd
index positions and 4 even index positions in an array
Even = 10 + 30 + 50 + 70 = 160
Odd = 20 + 40 + 60 = 120 
Recommended Practice

Implementation:


Output
Even index positions sum 9
Odd index positions sum 12

Time Complexity: O(length(arr))
Auxiliary Space: 0(1)

Method 2: Using bitwise & operator


Output
Even index positions sum 9
Odd index positions sum 12

Time Complexity: O(length(arr))
Auxiliary Space: 0(1)

Method 3: Using slicing in python:

Calculate sum of all even indices using slicing and repeat the same with odd indices and print sum.

Below is the implementation:


Output
Even index positions sum 9
Odd index positions sum 12

Time Complexity: O(n) where n is no of elements in given array
Auxiliary Space: 0(1)

Method 2: Using bitwise | operator


Output
Even index positions sum 9
Odd index positions sum 12

Time Complexity: O(length(arr))
Auxiliary Space: 0(1)

Comment