
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Sum of Alternative Elements of the Array Using C++
The sum of alternate elements has various real-life applications such as signal processing, data compression, pattern recognition, gaming, and financial data analysis.
Problem Description
In this article, we are going to learn how to find the sum of alternate elements of an array in C++. Alternate elements are present at even or odd indices of the array, depending on how we define them.
Example 1
-
Input
array = [1, 2, 3, 4, 5] -
Output
Sum_even = 9
Sum_odd = 6
Explanation
The alternate elements at the even index are 1, 3, 5 (at even indices 0, 2, 4), and elements at the odd index are 2, 4 (at odd indices 1, 3).
Example 2
-
Input
array = [10, 20, 30, 40, 50, 60] -
Output
Sum_even = 90
Sum_odd = 120
Explanation
The alternate elements at the even indices are 10, 30, and 50 (at indices 0, 2, and 4), and elements at the odd indices are 20, 40, and 60 (at indices 1, 3, 5).
Example 3
-
Input
array = [7, 14, 21, 28, 35] -
Output
Sum_even = 63
Sum_odd = 42
Explanation
The alternate elements at the even index are 7, 21, 35 (at even indices 0, 2, 4), and elements at the odd index are 14, 28 (at odd indices 1, 3).
Find the sum of alternative elements of array using for Loop
In this approach, we use a simple for loop to calculate the sum of alternate elements of a given array. To find the sum of elements at even Indices we will use a for loop to iterate over even indices.
Steps for Implementation
- First point the i = 0 in the loop as it points to the first even index.
- Now, increment i by 2 in each iteration to traverse only even indices.
- Add the current index value to sum_even, to find the sum of elements at even Indices.
- Now for the sum of odd indices, point the i =1 in the loop as it points to the first odd index.
- Now, increment i by 2 in each iteration to traverse only odd indices.
- Add the current index value to sum_odd.
- Finally, Print the value of sum_even and sum_odd.
Implementation Code
#include<bits/stdc++.h> using namespace std; int main() { int arr[] = { 1, 2, 3, 4, 5 }; int n = 5; int sum_even = 0; for (int i = 0; i < n; i += 2) { sum_even += arr[i]; } // Sum of elements at odd indices int sum_odd = 0; for (int i = 1; i < n; i += 2) { sum_odd += arr[i]; } // Output cout << "Sum of elements at even indices: " << sum_even << endl; cout << "Sum of elements at odd indices: " << sum_odd << endl; return 0; }
Output
Sum of elements at even indices: 9
Sum of elements at odd indices: 6
Time Complexity: O(n)
Space Complexity: O(1)
Find the sum of alternative elements of array using recursion
In this approach, we use a recursive approach to calculate the sum. We call it a recursive function that processes one element at a time and skips alternate elements.
Steps for Implementation
- First define a recursive function that takes the array, its size, the current index, and the sum so far.
- Now, add the element at the current index to the sum.
- Call the function recursively with the next alternate index.
- Return the final sum after traversing all elements.
Implementation Code
#include <iostream> using namespace std; // Recursive function to calculate the sum of alternate elements int sumAlternate(int arr[], int n, int index) { if (index >= n) { return 0; } return arr[index] + sumAlternate(arr, n, index + 2); // Add current element and recurse with the next alternate index } int main() { // Initialize array int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); int sum_even = sumAlternate(arr, n, 0); // Calculate sum of elements at odd indices (starting from index 1) int sum_odd = sumAlternate(arr, n, 1); // Output the results cout << "Sum of elements at even indices: " << sum_even << endl; cout << "Sum of elements at odd indices: " << sum_odd << endl; return 0; }
Output
Sum of elements at even indices: 9
Sum of elements at odd indices: 6
Time Complexity: O(n)
Space Complexity: O(n)
Real Life-Applications
-
Signal Processing
In signal processing, alternate elements represent samples at different phases or time intervals. -
Pattern Recognition
In image or speech processing, alternate elements can correspond to key features or characteristics in the data. -
Financial Data Analysis
In financial data, alternate elements is used to represent data points collected at odd or even time intervals.