数组中偶数和奇数索引元素的绝对差
给定一个整数数组arr ,任务是分别在偶数和奇数索引位置找到元素的运行绝对差。
注意:数组考虑基于 0 的索引。即数组中第一个元素的索引为零。
例子:
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: Even Index absolute difference : 3
Odd Index absolute difference : 4
Explanation :
Here, even indexed elements are 1, 3 and 5
So the even absolute difference will be (|1 – 3| = 2) => (|2 – 5| = 3)
Similarly odd indexed elements are 2, 4 and 6
And the odd absolute difference will be (|2 – 4| = 2) => (|2 – 6| = 4)
Input: arr[] = {10, 20, 30, 40, 50, 60, 70}
Output: Even Index absolute difference : 40
Odd Index absolute difference : 40
方法:遍历数组,保持偶数和奇数两个变量分别存储偶数和奇数索引元素的绝对差。遍历时检查当前索引是否为偶数,即(i%2) == 0 。最后打印结果。
下面是上述方法的实现:
C++
// CPP program to find absolute difference of elements
// at even and odd index positions separately
#include
using namespace std;
// Function to calculate absolute difference
void EvenOddAbsoluteDifference(int arr[], int n)
{
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
// Loop to find even, odd absolute difference
if (i % 2 == 0)
even = abs(even - arr[i]);
else
odd = abs(odd - arr[i]);
}
cout << "Even Index absolute difference : " << even;
cout << endl;
cout << "Odd Index absolute difference : " << odd;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
EvenOddAbsoluteDifference(arr, n);
return 0;
}
Java
// Java program to find absolute difference of elements
// at even and odd index positions separately
public class GFG{
// Function to calculate absolute difference
static void EvenOddAbsoluteDifference(int arr[], int n)
{
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
// Loop to find even, odd absolute difference
if (i % 2 == 0)
even = Math.abs(even - arr[i]);
else
odd = Math.abs(odd - arr[i]);
}
System.out.println("Even Index absolute difference : " + even);
System.out.println("Odd Index absolute difference : " + odd);
}
// Driver Code
public static void main(String []args){
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = arr.length;
EvenOddAbsoluteDifference(arr, n);
}
// This code is contributed by ANKITRAI1
}
Python3
# Python 3 program to find absolute difference of
# elements at even and odd index positions separately
# Function to calculate absolute difference
def EvenOddAbsoluteDifference(arr, n):
even = 0
odd = 0
for i in range(0, n, 1):
# Loop to find even, odd absolute
# difference
if (i % 2 == 0):
even = abs(even - arr[i]);
else:
odd = abs(odd - arr[i]);
print("Even Index absolute difference :", even)
print("Odd Index absolute difference :", odd)
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5, 6]
n = len(arr)
EvenOddAbsoluteDifference(arr, n)
# This code is contributed by
# Sahil_shelangia
C#
// C# program to find absolute difference
// of elements at even and odd index
// positions separately
using System;
class GFG
{
// Function to calculate absolute difference
static void EvenOddAbsoluteDifference(int []arr,
int n)
{
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++)
{
// Loop to find even, odd
// absolute difference
if (i % 2 == 0)
even = Math.Abs(even - arr[i]);
else
odd = Math.Abs(odd - arr[i]);
}
Console.WriteLine("Even Index absolute " +
"difference : " + even);
Console.WriteLine("Odd Index absolute " +
"difference : " + odd);
}
// Driver Code
static public void Main ()
{
int []arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.Length;
EvenOddAbsoluteDifference(arr, n);
}
}
// This code is contributed by Sachin
PHP
Javascript
Even Index absolute difference : 3
Odd Index absolute difference : 4
时间复杂度: O(n)