给定一个数组arr[]和一个数字K ,任务是找到K 个最大偶数和奇数数组元素之和的绝对差。
注意:数组中至少分别存在K 个偶数和奇数元素。
例子:
Input arr[] = {1, 2, 3, 4, 5, 6}, K = 2
Output: 2
Explanation:
The 2 maximum even numbers are 6, 4. The sum is 6 + 4 = 10.
The 2 maximum odd numbers are 5, 3. The sum is 5 + 3 = 8.
Difference = 10 – 8 = 2.
Input arr[] = {1, 8, 4, 5, 6, 3}, K = 3
Output: 4
Explanation:
The 3 maximum even numbers are 8, 6, 4. The sum is 8 + 6 + 4 = 18.
The 3 maximum odd numbers are 5, 3, 1. The sum is 5 + 3 + 1 = 9.
Difference = 18 – 9 = 9.
朴素的方法:最简单的方法是通过遍历数组找到K个最大偶数和K个最大奇数,并打印得到的K个最大偶数和奇数元素之和的绝对差。
时间复杂度: O(N*K)
辅助空间: O(1)
高效的方法:对上述方法进行优化,其思想是利用将数组分成奇数和偶数的概念,然后将数组按降序排序为分别包含偶数和奇数的两部分。请按照以下步骤解决问题:
- 分别在给定数组中分离偶数和奇数,并存储从奇数开始的索引。
- 让奇数开始的索引为K 。按降序对[0, K – 1]和[K, N – 1]范围内的数字进行排序。
- 从阵列的开始和从那里奇数开始点前K数的和是和前K最大偶数和奇数阵列中分别。
- 打印在上述步骤中计算的总和之间的绝对差作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the absolute
// difference between sum of first K
// maximum even and odd numbers
void evenOddDiff(int a[], int n, int k)
{
// Stores index from where odd
// number starts
int j = -1;
// Segregate even and odd number
for (int i = 0; i < n; i++) {
// If current element is even
if (a[i] % 2 == 0) {
j++;
swap(a[i], a[j]);
}
}
j++;
// Sort in decreasing order even part
sort(a, a + j, greater());
// Sort in decreasing order odd part
sort(a + j, a + n, greater());
int evenSum = 0, oddSum = 0;
// Calculate sum of k
// maximum even number
for (int i = 0; i < k; i++) {
evenSum += a[i];
}
// Calculate sum of k
// maximum odd number
for (int i = j; i < (j + k); i++) {
oddSum += a[i];
}
// Print the absolute difference
cout << abs(evenSum - oddSum);
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 8, 3, 4, 5 };
// Size of array
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
// Function Call
evenOddDiff(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the absolute
// difference between sum of first K
// maximum even and odd numbers
static void evenOddDiff(int a[], int n, int k)
{
// Stores index from where odd
// number starts
int j = -1;
Vector even = new Vector<>();
Vector odd = new Vector<>();
// Segregate even and odd number
for(int i = 0; i < n; i++)
{
// If current element is even
if (a[i] % 2 == 0)
{
even.add(a[i]);
}
else
odd.add(a[i]);
}
j++;
// Sort in decreasing order even part
Collections.sort(even);
Collections.reverse(even);
// Sort in decreasing order odd part
Collections.sort(odd);
Collections.reverse(odd);
int evenSum = 0, oddSum = 0;
// Calculate sum of k
// maximum even number
for(int i = 0; i < k; i++)
{
evenSum += even.get(i);
}
// Calculate sum of k
// maximum odd number
for(int i = 0; i < k; i++)
{
oddSum += odd.get(i);
}
// Print the absolute difference
System.out.print(Math.abs(evenSum - oddSum));
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 1, 8, 3, 4, 5 };
// Size of array
int N = arr.length;
int K = 2;
// Function Call
evenOddDiff(arr, N, K);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function to find the absolute
# difference between sum of first K
# maximum even and odd numbers
def evenOddDiff(a, n, k) :
# Stores index from where odd
# number starts
j = -1
even = []
odd = []
# Segregate even and odd number
for i in range(n) :
# If current element is even
if (a[i] % 2 == 0) :
even.append(a[i])
else :
odd.append(a[i])
j += 1
# Sort in decreasing order even part
even.sort()
even.reverse()
# Sort in decreasing order odd part
odd.sort()
odd.reverse()
evenSum, oddSum = 0, 0
# Calculate sum of k
# maximum even number
for i in range(k) :
evenSum += even[i]
# Calculate sum of k
# maximum odd number
for i in range(k) :
oddSum += odd[i]
# Print the absolute difference
print(abs(evenSum - oddSum))
# Given array []arr
arr = [ 1, 8, 3, 4, 5 ]
# Size of array
N = len(arr)
K = 2
# Function Call
evenOddDiff(arr, N, K)
# This code is contributed by divyeshrabadiya07
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the absolute
// difference between sum of first K
// maximum even and odd numbers
static void evenOddDiff(int []a, int n,
int k)
{
// Stores index from where odd
// number starts
int j = -1;
List even = new List();
List odd = new List();
// Segregate even and odd number
for(int i = 0; i < n; i++)
{
// If current element is even
if (a[i] % 2 == 0)
{
even.Add(a[i]);
}
else
odd.Add(a[i]);
}
j++;
// Sort in decreasing order even part
even.Sort();
even.Reverse();
// Sort in decreasing order odd part
odd.Sort();
odd.Reverse();
int evenSum = 0, oddSum = 0;
// Calculate sum of k
// maximum even number
for(int i = 0; i < k; i++)
{
evenSum += even[i];
}
// Calculate sum of k
// maximum odd number
for(int i = 0; i < k; i++)
{
oddSum += odd[i];
}
// Print the absolute difference
Console.Write(Math.Abs(evenSum - oddSum));
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = { 1, 8, 3, 4, 5 };
// Size of array
int N = arr.Length;
int K = 2;
// Function Call
evenOddDiff(arr, N, K);
}
}
// This code is contributed by Amit Katiyar
4
时间复杂度: O(N*log N + K)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live