Absolute Difference of even and odd indexed elements in an Array - GeeksforGeeks

28 May, 2021
Given an array of integers arr, the task is to find the running absolute difference of elements at even and odd index positions separately. Note: 0-based indexing is considered for the array. That is the index of the first element in the array is zero. Examples:  
Input: arr[] = {1, 2, 3, 4, 5, 6} Output: Even Index absolute difference : 3 Odd Index absolute difference : 4 Explanation : Here, even indexed elements are 1, 3 and 5 So the even absolute difference will be (|1 – 3| = 2) => (|2 – 5| = 3) Similarly odd indexed elements are 2, 4 and 6 And the odd absolute difference will be (|2 – 4| = 2) => (|2 – 6| = 4)Input: arr[] = {10, 20, 30, 40, 50, 60, 70} Output: Even Index absolute difference : 40 Odd Index absolute difference : 40  
 
Approach: Traverse the array and keep two variables even and odd to store the absolute difference of elements of even and odd indexes respectively. While traversing check if the current index is even, i.e. (i%2) == 0. Print the results in the end.Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find absolute difference of elements// at even and odd index positions separately#include <bits/stdc++.h>using namespace std; // Function to calculate absolute differencevoid EvenOddAbsoluteDifference(int arr[], int n){    int even = 0;    int odd = 0;     for (int i = 0; i < n; i++) {         // Loop to find even, odd absolute difference        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;} // Driver Codeint main(){    int arr[] = { 1, 2, 3, 4, 5, 6 };    int n = sizeof(arr) / sizeof(arr[0]);     EvenOddAbsoluteDifference(arr, n);     return 0;}
// Java program to find absolute difference of elements// at even and odd index positions separately public class GFG{         // Function to calculate absolute difference    static void EvenOddAbsoluteDifference(int arr[], int n)    {        int even = 0;        int odd = 0;             for (int i = 0; i < n; i++) {                 // Loop to find even, odd absolute difference            if (i % 2 == 0)                even = Math.abs(even - arr[i]);            else                odd = Math.abs(odd - arr[i]);        }             System.out.println("Even Index absolute difference : " + even);        System.out.println("Odd Index absolute difference : " + odd);    }       // Driver Code     public static void main(String []args){                        int arr[] = { 1, 2, 3, 4, 5, 6 };              int n = arr.length;              EvenOddAbsoluteDifference(arr, n);     }     // This code is contributed by ANKITRAI1}
# Python 3 program to find absolute difference of# elements at even and odd index positions separately # Function to calculate absolute differencedef EvenOddAbsoluteDifference(arr, n):    even = 0    odd = 0     for i in range(0, n, 1):                 # Loop to find even, odd absolute        # difference        if (i % 2 == 0):            even = abs(even - arr[i]);        else:            odd = abs(odd - arr[i]);     print("Even Index absolute difference :", even)    print("Odd Index absolute difference :", odd) # Driver Codeif __name__ == '__main__':    arr = [1, 2, 3, 4, 5, 6]    n = len(arr)     EvenOddAbsoluteDifference(arr, n)     # This code is contributed by# Sahil_shelangia
// C# program to find absolute difference// of elements at even and odd index// positions separatelyusing System; class GFG{// Function to calculate absolute differencestatic void EvenOddAbsoluteDifference(int []arr,                                      int n){    int even = 0;    int odd = 0;     for (int i = 0; i < n; i++)    {         // Loop to find even, odd        // absolute difference        if (i % 2 == 0)            even = Math.Abs(even - arr[i]);        else            odd = Math.Abs(odd - arr[i]);    }     Console.WriteLine("Even Index absolute " +                      "difference : " + even);    Console.WriteLine("Odd Index absolute " +                      "difference : " + odd);} // Driver Codestatic public void Main (){    int []arr = { 1, 2, 3, 4, 5, 6 };    int n = arr.Length;    EvenOddAbsoluteDifference(arr, n);}} // This code is contributed by Sachin
<?php// PHP program to find absolute// difference of elements at even// and odd index positions separately // Function to calculate absolute// differencefunction EvenOddAbsoluteDifference($arr, $n){    $even = 0;    $odd = 0;     for ($i = 0; $i < $n; $i++)    {         // Loop to find even, odd        // absolute difference        if ($i % 2 == 0)            $even = abs($even - $arr[$i]);        else            $odd = abs($odd - $arr[$i]);    }     echo "Even Index absolute difference : ",                                       $even;    echo "\n";    echo "Odd Index absolute difference : ",                                       $odd;} // Driver Code$arr = array( 1, 2, 3, 4, 5, 6 );$n = sizeof($arr); EvenOddAbsoluteDifference($arr, $n); // This code is contributed by Sachin?>
<script>     // Javascript program to find absolute difference    // of elements at even and odd index    // positions separately         // Function to calculate absolute difference    function EvenOddAbsoluteDifference(arr, n)    {        let even = 0;        let odd = 0;         for (let i = 0; i < n; i++)        {             // Loop to find even, odd            // absolute difference            if (i % 2 == 0)                even = Math.abs(even - arr[i]);            else                odd = Math.abs(odd - arr[i]);        }         document.write("Even Index absolute " +                          "difference : " + even + "</br>");        document.write("Odd Index absolute " +                          "difference : " + odd + "</br>");    }         let arr = [ 1, 2, 3, 4, 5, 6 ];    let n = arr.length;    EvenOddAbsoluteDifference(arr, n);     </script>
Even Index absolute difference : 3
Odd Index absolute difference : 4
 
Time complexity : O(n) 
ankthon
Sach_Code
VishalBachchas
sahilshelangia
rameshtravel07
array-traversal-question
Technical Scripter 2018
Arrays
Technical Scripter
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Count pairs with given sum
Find subarray with given sum | Set 1 (Nonnegative Numbers)
Building Heap from Array
Remove duplicates from sorted array
Program to find sum of elements in a given array