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: 0
Explanation: Sum of even elements at even indices: 4 {4}
Sum of odd elements at odd indices: 4 {3, 1}
Absolute Difference = 4-4 = 0Input: 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++
// 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
// 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 |
Python3
# 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#
// 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. |
Javascript
<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)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
