📌  相关文章
📜  K个最大偶数和奇数数组元素之和之间的差

📅  最后修改于: 2021-05-07 07:57:02             🧑  作者: Mango

给定一个数组arr []和一个数字K ,任务是找到K个最大偶数奇数数组元素之和的绝对差。
注意:数组中至少存在K个偶数和奇数元素。

例子:

天真的方法:最简单的方法是遍历数组以找到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)