Absolute difference between sum of even elements at even indices & odd elements at odd indices in given Array - GeeksforGeeks

13 Dec, 2021
Given an array arr[] containing N elements, the task is to find the absolute difference between the sum of even elements at even indices & the count of odd elements at odd indices. Consider 1-based indexing
Examples:
Input: arr[] = {3, 4, 1, 5}Output: 0Explanation: Sum of even elements at even indices: 4 {4}Sum of odd elements at odd indices: 4 {3, 1}Absolute Difference = 4-4 = 0
Input: arr[] = {4, 2, 1, 3}Output: 1
Approach: The task can be solved by traversing the array from left to right, keeping track of sum of odd and even elements at odd & even indices respectively. Follow the steps below to solve the problem:
Traverse the array from left to right.
If the current index is even, check whether the element at that index is even or not, If it’s even, add it to the sum evens.
If the current index is odd, check whether the element at that index is odd or not, If it’s odd, add it to the sum odds.
Return the absolute difference between odds and evens.
Below is the implementation of the above approach.
C++
Java
Python3
C#
Javascript
// C++ program to implement the above approach#include <bits/stdc++.h>using namespace std; // Function to find the required absolute differenceint xorOr(int arr[], int N){    // Store the count of odds & evens at odd    // and even indices respectively    int evens = 0, odds = 0;     // Traverse the array to count even/odd    for (int i = 0; i < N; i++) {        if ((i + 1) % 2 == 0            && arr[i] % 2 == 0)            evens += arr[i];        else if ((i + 1) % 2 != 0                 && arr[i] % 2 != 0)            odds += arr[i];    }     return abs(odds - evens);} // Driver Codeint main(){    int arr[] = { 3, 4, 1, 5 };    int N = sizeof(arr) / sizeof(arr[0]);    cout << xorOr(arr, N);    return 0;}
// Java code for the above approachimport java.util.*;class GFG{   // Function to find the required absolute differencestatic int xorOr(int arr[], int N){    // Store the count of odds & evens at odd    // and even indices respectively    int evens = 0, odds = 0;     // Traverse the array to count even/odd    for (int i = 0; i < N; i++) {        if ((i + 1) % 2 == 0            && arr[i] % 2 == 0)            evens += arr[i];        else if ((i + 1) % 2 != 0                 && arr[i] % 2 != 0)            odds += arr[i];    }     return Math.abs(odds - evens);} // Driver Code    public static void main (String[] args) {       int arr[] = { 3, 4, 1, 5 };    int N = arr.length;              System.out.println(xorOr(arr, N));    }} // This code is contributed by Potta Lokesh
# Python code for the above approach # Function to find the required absolute differencedef xorOr(arr, N):       # Store the count of odds & evens at odd    # and even indices respectively    evens = 0;    odds = 0;     # Traverse the array to count even/odd    for i in range(N):        if ((i + 1) % 2 == 0 and arr[i] % 2 == 0):            evens += arr[i];        elif ((i + 1) % 2 != 0 and arr[i] % 2 != 0):            odds += arr[i];     return abs(odds - evens); # Driver Codeif __name__ == '__main__':    arr = [3, 4, 1, 5];    N = len(arr);     print(xorOr(arr, N)); # This code is contributed by 29AjayKumar
// C# code for the above approachusing System;using System.Collections;class GFG{   // Function to find the required absolute differencestatic int xorOr(int []arr, int N){       // Store the count of odds & evens at odd    // and even indices respectively    int evens = 0, odds = 0;     // Traverse the array to count even/odd    for (int i = 0; i < N; i++) {        if ((i + 1) % 2 == 0            && arr[i] % 2 == 0)            evens += arr[i];        else if ((i + 1) % 2 != 0                 && arr[i] % 2 != 0)            odds += arr[i];    }     return Math.Abs(odds - evens);} // Driver Codepublic static void Main () {    int []arr = { 3, 4, 1, 5 };    int N = arr.Length;              Console.Write(xorOr(arr, N));}} // This code is contributed by Samim Hossain Mondal.
<script>// Javascript program to implement the above approach  // Function to find the required absolute differencefunction xorOr(arr, N) {    // Store the count of odds & evens at odd    // and even indices respectively    let evens = 0, odds = 0;     // Traverse the array to count even/odd    for (let i = 0; i < N; i++) {        if ((i + 1) % 2 == 0            && arr[i] % 2 == 0)            evens += arr[i];        else if ((i + 1) % 2 != 0            && arr[i] % 2 != 0)            odds += arr[i];    }     return Math.abs(odds - evens);} // Driver Codelet arr = [3, 4, 1, 5];let N = arr.length;document.write(xorOr(arr, N)); // This code is contributed by gfgking.</script>
  
0
 
Time Complexity: O(N)Auxiliary Space: O(1)
 
lokeshpotta20
samim2000
gfgking
29AjayKumar
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Window Sliding Technique
Program to find sum of elements in a given array
Move all negative numbers to beginning and positive to end with constant extra space
Reversal algorithm for array rotation
Find duplicates in O(n) time and O(1) extra space | Set 1
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Program to find GCD or HCF of two numbers