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 = 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++
#include <bits/stdc++.h>
using namespace std;
int xorOr( int arr[], int N)
{
int evens = 0, odds = 0;
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);
}
int main()
{
int arr[] = { 3, 4, 1, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << xorOr(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int xorOr( int arr[], int N)
{
int evens = 0 , odds = 0 ;
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);
}
public static void main (String[] args) {
int arr[] = { 3 , 4 , 1 , 5 };
int N = arr.length;
System.out.println(xorOr(arr, N));
}
}
|
Python3
def xorOr(arr, N):
evens = 0 ;
odds = 0 ;
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);
if __name__ = = '__main__' :
arr = [ 3 , 4 , 1 , 5 ];
N = len (arr);
print (xorOr(arr, N));
|
C#
using System;
using System.Collections;
class GFG
{
static int xorOr( int []arr, int N)
{
int evens = 0, odds = 0;
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);
}
public static void Main () {
int []arr = { 3, 4, 1, 5 };
int N = arr.Length;
Console.Write(xorOr(arr, N));
}
}
|
Javascript
<script>
function xorOr(arr, N) {
let evens = 0, odds = 0;
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);
}
let arr = [3, 4, 1, 5];
let N = arr.length;
document.write(xorOr(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Dec, 2021
Like Article
Save Article