给定数组中偶数索引处的偶数元素和奇数索引处的奇数元素之和之间的绝对差
给定一个包含N个元素的数组arr[] ,任务是找到偶数索引处的偶数元素之和与奇数索引处奇数元素的计数之间的绝对差。考虑基于 1 的索引
例子:
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
方法:可以通过从左到右遍历数组来解决该任务,分别跟踪奇数和偶数索引处的奇数和偶数元素的总和。请按照以下步骤解决问题:
- 从左到右遍历数组。
- 如果当前索引为偶数,则检查该索引处的元素是否为偶数,如果是偶数,则将其添加到总和中。
- 如果当前索引为奇数,则检查该索引处的元素是否为奇数,如果为奇数,则将其添加到总赔率中。
- 返回赔率和偶数之间的绝对差。
下面是上述方法的实现。
C++
// C++ program to implement the above approach
#include
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
输出
0
时间复杂度: O(N)
辅助空间: O(1)