
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
Absolute Difference of even and odd indexed elements in an Array in C++?
An array in C++ is a collection of elements of the same data type stored in contiguous memory locations. Arrays provide a way to store multiple values using a single variable name, and each element can be accessed using an index starting from 0.
Even and Odd Indexed Elements in an Array
Each element in an array has an index. Indexes that are divisible by 2 (like 0, 2, 4, etc.) are known as even indexes, while indexes that are not divisible by 2 (like 1, 3, 5, etc.) are referred to as odd indexes.
For example, in the array {10, 20, 30, 40, 50}
:
- Even indexed elements: 10 (index 0), 30 (index 2), 50 (index 4)
- Odd indexed elements: 20 (index 1), 40 (index 3)
We are given an array of integers, and our task is to calculate the absolute difference of all elements at even index positions and all elements at odd index positions separately. The result should display two values, one for even indexed elements and one for odd indexed elements.
Example Scenario
Consider the following example scenario to under the concept better:
Input: arr = {1, 2, 4, 5, 8} Output: Absolute difference of even indexed elements = 4 Absolute difference of odd indexed elements = 3 Explanation: Even indexed elements: 1, 4, 8 = |4 - 1| = 3, |8 - 4| = 4 Odd indexed elements: 2, 5 = |5 - 2| = 3
Calculating Absolute Difference of Even and Odd Indexed Elements in an Array
The absolute difference between two numbers is the positive value of their subtraction, regardless of the order. It is denoted by |a - b|
. For example, |7 - 3| = 4
and |3 - 7| = 4
.
To compute the absolute difference of elements based on their index positions in a C++ array, we follow this approach:
- Initialize two variables to store the absolute differences of even-indexed and odd-indexed elements separately.
- Traverse the array using a loop.
- For each element, check its index:
If the index is even (i.e.,i % 2 == 0
), update the even index difference using theabs()
function. If the index is odd, update the odd index difference similarly. - Continue this process for all elements in the array.
This technique allows us to calculate the total absolute difference of elements at even and odd positions independently, which can then be displayed as part of the program's output.
C++ Program to Find Absolute Difference of Even and Odd Indexed Elements in an Array
#include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 1, 5, 8, 10, 15, 26 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "The array is:"; for (int i = 0; i < n; i++) { cout << " " << arr[i]; } cout << endl; int even = 0; int odd = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) even = abs(even - arr[i]); else odd = abs(odd - arr[i]); } cout << "Even index absolute difference: " << even << endl; cout << "Odd index absolute difference: " << odd << endl; return 0; }
Output
The array is: 1 5 8 10 15 26 Even index absolute difference: 8 Odd index absolute difference: 21