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 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> |
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!