
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Absolute Difference of even and odd indexed elements in an Array (C++)?
Here we will see how we can get the absolute differences of odd and even indexed elements in an array. The absolute difference indicates that if the difference of one pair is negative, the absolute value will be taken. For an example, let the numbers are {1, 2, 3, 4, 5, 6, 7, 8, 9}. So the even position elements are 1, 3, 5, 7, 9 (starting from 0), and odd place elements are 2, 4, 6, 8. So the difference for even placed data are |1 - 3| = 2, then |2 - 5| = 3, |3 - 7| = 4 and |4 - 9| = 5 similarly the differences of odd number of places will be 4.
Algorithm
offEvenDiff(arr, n)
begin even := 0 odd := 0 for i := 0 to n-1, do if i is even, then even := |even – arr[i]| else odd := |odd – arr[i]| done return (odd,even) end
Example
#include<iostream> #include<cmath> using namespace std; void oddEvenDiff(int arr[], int n, int &o, int &e) { int even = 0; int odd = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { even = abs(even - arr[i]); //get the even difference } else { odd = abs(odd - arr[i]); } } e = even; o = odd; } main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = sizeof(arr) / sizeof(arr[0]); int odd, even; oddEvenDiff(arr, n, odd, even); cout << "The odd and even differences are: " << odd << " and " << even; }
Output
The odd and even differences are: 4 and 5
- Related Articles
- Absolute Difference of even and odd indexed elements in an Array in C++?
- JavaScript Sum odd indexed and even indexed elements separately and return their absolute difference
- Count number of even and odd elements in an array in C++
- Find elements of an Array which are Odd and Even using STL in C++
- Program to find number of elements can be removed to make odd and even indexed elements sum equal in Python
- Absolute Difference of all pairwise consecutive elements in an array (C++)?
- Odd even sort in an array - JavaScript
- Converting Odd and Even-indexed characters in a string to uppercase/lowercase in JavaScript?
- Swap Even Index Elements And Odd Index Elements in Python
- Sorting odd and even elements separately JavaScript
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Difference between sums of odd and even digits.
- Find if sum of odd or even array elements are smaller in Java
- Count maximum elements of an array whose absolute difference does not exceed K in C++
- Java program to Print Odd and Even Number from an Array

Advertisements