Sum of Elements at Odd Indices in an Array
Problem Statement
Given an array of integers, calculate the sum of elements present at odd indices.
Note:
Odd indices refer to index positions, not odd-valued elements.
Array indexing always starts from 0.
Understanding Odd Indices
In an array:
- Index 0 → even
- Index 1 → odd
- Index 2 → even
- Index 3 → odd
- Index 5, 7, 9, … → odd indices
So, we consider elements at indices:
1, 3, 5, 7, ...
Example
Input
Array: [5, 15, 25, 35, 45]
Index Mapping
Index: 0 1 2 3 4
Element: 5 15 25 35 45
Elements at Odd Indices
Index 1 → 15
Index 3 → 35
Output
Sum = 15 + 35 = 50
Why This Problem Is Important
This problem helps in understanding:
- Index-based filtering
- Conditional checks within loops
- Difference between index and element properties
- Logical thinking for selective traversal
It also prepares you for:
- Alternate index problems
- Range-based queries
- Prefix sum optimizations
Approach / Logic
- Initialize a variable sum = 0
- Traverse the array using a loop
- Check if the index is odd (i % 2 != 0)
- Add the element at that index to sum
- Print the final sum
Step-by-Step Algorithm
- Start
- Read the array and its size
- Initialize sum = 0
- Loop from i = 0 to n - 1
- If i % 2 != 0, add arr[i] to sum
- Print sum
- End
Pseudocode
sum = 0
for i = 0 to n-1:
if i % 2 != 0:
sum = sum + arr[i]
print sum
Dry Run Example
Array = [2, 4, 6, 8, 10, 12]
i = 0 → even → skip
i = 1 → odd → sum = 4
i = 2 → even → skip
i = 3 → odd → sum = 4 + 8 = 12
i = 4 → even → skip
i = 5 → odd → sum = 12 + 12 = 24
Final Sum = 24
Time and Space Complexity
| Metric | Value |
|---|---|
| Time Complexity | O(n) |
| Space Complexity | O(1) |
Only a single traversal is required, with no extra memory usage.
Language-wise Implementation
C Implementation
#include
int main() {
int arr[] = {5, 15, 25, 35, 45};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for(int i = 0; i < n; i++) {
if(i % 2 != 0) {
sum += arr[i];
}
}
printf("Sum of elements at odd indices: %d", sum);
return 0;
}
Output
Sum of elements at odd indices: 50
C++ Implementation
#include
using namespace std;
int main() {
int arr[] = {5, 15, 25, 35, 45};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for(int i = 0; i < n; i++) {
if(i % 2 != 0) {
sum += arr[i];
}
}
cout << "Sum of elements at odd indices: " << sum;
return 0;
}
Output
Sum of elements at odd indices: 50
Java Implementation
public class Main {
public static void main(String[] args) {
int[] arr = {5, 15, 25, 35, 45};
int sum = 0;
for(int i = 0; i < arr.length; i++) {
if(i % 2 != 0) {
sum += arr[i];
}
}
System.out.println("Sum of elements at odd indices: " + sum);
}
}
Output
Sum of elements at odd indices: 50
Python Implementation
arr = [5, 15, 25, 35, 45]
sum_odd_index = 0
for i in range(len(arr)):
if i % 2 != 0:
sum_odd_index += arr[i]
print("Sum of elements at odd indices:", sum_odd_index)
Output
Sum of elements at odd indices: 50
C# Implementation
using System;
class Program {
static void Main() {
int[] arr = {5, 15, 25, 35, 45};
int sum = 0;
for(int i = 0; i < arr.Length; i++) {
if(i % 2 != 0) {
sum += arr[i];
}
}
Console.WriteLine("Sum of elements at odd indices: " + sum);
}
}
Output
Sum of elements at odd indices: 50
JavaScript Implementation
let arr = [5, 15, 25, 35, 45];
let sum = 0;
for (let i = 0; i < arr.length; i++) {
if (i % 2 !== 0) {
sum += arr[i];
}
}
console.log("Sum of elements at odd indices:", sum);
Output
Sum of elements at odd indices: 50
Common Mistakes to Avoid
- Confusing odd indices with odd values
- Starting indexing from 1
- Forgetting index 1
- Using unnecessary extra arrays
Interview Variations
- Compare sum of even vs odd indices
- Find maximum sum among even or odd indices
- Prefix sum of odd index elements
- Alternate index summation
Detailed Summary
The problem of summing elements at odd indices reinforces the importance of index-based operations in arrays. By focusing on position rather than value, this problem strengthens loop control, conditional logic, and array traversal fundamentals. It also builds a solid base for more advanced concepts such as range-based calculations, prefix sums, and sliding window techniques, which frequently rely on selective index processing.
