
- 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 in C++?
An array is a container of multiple elements of the same data type. The index of elements start from 0 i.e. first element has index 0.
In this problem, we need to find the absolute difference between two even indexed number and two odd indexed numbers.
Even indexed number = 0,2,4,6,8….
Odd indexed number = 1,3,5,7,9…
Absolute difference is the modulus of difference between two elements.
For example,
Absolute difference of 15 and 7 = (|15 - 7|) = 8
Input: arr = {1 , 2, 4, 5, 8} Output : Absolute difference of even numbers = 4 Absolute difference of odd numbers = 3
Explanation
Even elements are 1, 4,8
Absolute difference is
(|4 - 1|) = 3 and (|8 - 4|) = 4
Odd elements are 2,5
Absolute difference is
(|5- 2|) = 3
Example
#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 : \n"; for(int i = 0;i < n;i++){ cout<<" "<<arr[i]; 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; cout << endl; cout << "Odd Index absolute difference : " << odd; return 0; } }
Output
The array is : 1 5 8 10 15 26 Even index absolute difference : 8 Odd index absolute difference : 21
- Related Articles
- Absolute Difference of even and odd indexed elements in an Array (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
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- 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++
- How to find the Odd and Even numbers in an Array in java?
- Sorting odd and even elements separately JavaScript
- Program to find only even indexed elements from list in Python

Advertisements