Absolute difference between sum of even elements at even indices & odd elements at odd indices in given Array
Last Updated :
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: 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++
// C++ program to implement the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the required absolute difference
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 abs(odds - evens);
}
// Driver Code
int 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 approach
import java.util.*;
class GFG
{
// Function to find the required absolute difference
static 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 difference
def 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 Code
if __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 approach
using System;
using System.Collections;
class GFG
{
// Function to find the required absolute difference
static 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 () {
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 difference
function 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 Code
let arr = [3, 4, 1, 5];
let N = arr.length;
document.write(xorOr(arr, N));
// This code is contributed by gfgking.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Difference between sum of odd and even frequent elements in an Array Given an array arr[] of integers, the task is to find the absolute difference between the sum of all odd frequent array elements and the sum of all even frequent array elements. Examples: Input: arr[] = {1, 5, 5, 2, 4, 3, 3} Output: 9 Explanation: The even frequent elements are 5 and 3 (both occurri
12 min read
Find the Array Permutation having sum of elements at odd indices greater than sum of elements at even indices Given an array arr[] consisting of N integers, the task is to find the permutation of array elements such that the sum of odd indices elements is greater than or equal to the sum of even indices elements. Examples: Input: arr[] = {1, 2, 3, 4}Output: 1 4 2 3 Explanation:Consider the permutation of th
6 min read
Absolute Difference of even and odd indexed elements in an Array 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} Out
6 min read
Maximize the difference of sum of elements at even indices and odd indices by shifting an odd sized subarray to end of given Array. Given an array arr[] of size N, the task is to maximize the difference of the sum of elements at even indices and elements at odd indices by shifting any subarray of odd length to the end of the array. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}Output: 3Explanation: Initially sum of elements at even
13 min read
Difference between sum of K maximum even and odd array elements Given an array arr[] and a number K, the task is to find the absolute difference of the sum of K maximum even and odd array elements.Note: At least K even and odd elements are present in the array respectively. Examples: Input arr[] = {1, 2, 3, 4, 5, 6}, K = 2Output: 2Explanation:The 2 maximum even
8 min read