📌  相关文章
📜  最大化由与数组两端等距的元素组成的 K 对的总和

📅  最后修改于: 2021-10-25 10:33:42             🧑  作者: Mango

给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是找到形式为(arr[i], arr[N – i – 1])K对的最大和,其中(0 ≤ i ≤ N – 1)

例子:

朴素方法:解决问题的最简单方法是从数组中生成给定形式的所有可能对,并计算 K 个此类对的最大和。检查所有对后,打印所有可能和中获得的最大和。

时间复杂度: O(N 2 * K)
辅助空间: O(N 2 )

高效方法:上述方法可以贪婪地优化。这个想法是只选择成本最高的 K 对。请按照以下步骤解决问题:

  • 初始化一个向量,比如pairwiseSum[] ,以存储数组中所需对的总和。
  • 通过遍历数组从给定数组生成对(arr[i], arr[N – i + 1]) 。将它们的总和存储在向量pairwiseSum[] 中
  • 按降序对向量pairwiseSum[]进行排序。
  • 完成上述步骤后,打印向量pairwiseSum[]的前K 个元素的总和作为结果总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum sum of
// K pairs of the form (arr[i],
// arr[N - i - 1])
int maxSum(int arr[], int N, int K)
{
    // Stores the resultant pairwise
    // sum
    vector pairwiseSum;
 
    // Traverse till half of the array
    for (int i = 0; i < N / 2; i++) {
 
        // Update the value of curSum
        int curSum = arr[i] + arr[i + N / 2];
        pairwiseSum.push_back(curSum);
    }
 
    // Sort in the descending order
    sort(pairwiseSum.begin(),
         pairwiseSum.end(),
         greater());
 
    // Stores the resultant maximum sum
    int maxSum = 0;
 
    // Add K maximum sums obtained
    for (int i = 0; i < K; i++) {
 
        // Update the value of maxSum
        maxSum += pairwiseSum[i];
    }
 
    // Print the maximum sum
    cout << maxSum;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, -4, 3, 5, 2, -1 };
    int K = 2;
    int N = sizeof(arr) / sizeof(arr[0]);
    maxSum(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.Collections;
class GFG{
 
// Function to find the maximum sum of
// K pairs of the form (arr[i],
// arr[N - i - 1])
public static void maxSum(int arr[], int N, int K)
{
    // Stores the resultant pairwise
    // sum
    ArrayList pairwiseSum = new ArrayList();
 
    // Traverse till half of the array
    for (int i = 0; i < N / 2; i++) {
 
        // Update the value of curSum
        int curSum = arr[i] + arr[i + N / 2];
        pairwiseSum.add(curSum);
    }
 
    // Sort in the descending order
    Collections.sort(pairwiseSum);
    Collections.reverse(pairwiseSum);
 
    // Stores the resultant maximum sum
    int maxSum = 0;
 
    // Add K maximum sums obtained
    for (int i = 0; i < K; i++) {
 
        // Update the value of maxSum
        maxSum += pairwiseSum.get(i);
    }
 
    // Print the maximum sum
    System.out.println(maxSum);
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 2, -4, 3, 5, 2, -1 };
    int K = 2;
    int N = arr.length;
    maxSum(arr, N, K);
}
}
 
// This code is contributed by gfgking.


Python3
# Python3 program for the above approach
 
# Function to find the maximum sum of
# K pairs of the form (arr[i],
# arr[N - i - 1])
def maxSum(arr, N, K):
     
    # Stores the resultant pairwise
    # sum
    pairwiseSum = []
 
    # Traverse till half of the array
    for i in range(N // 2):
         
        # Update the value of curSum
        curSum = arr[i] + arr[i + N // 2]
        pairwiseSum.append(curSum)
 
    # Sort in the descending order
    pairwiseSum.sort(reverse = True)
 
    # Stores the resultant maximum sum
    maxSum = 0
 
    # Add K maximum sums obtained
    for i in range(K):
         
        # Update the value of maxSum
        maxSum += pairwiseSum[i]
 
    # Print the maximum sum
    print(maxSum)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 2, -4, 3, 5, 2, -1 ]
    K = 2
    N = len(arr)
     
    maxSum(arr, N, K)
 
# This code is contributed by bgangwar59


C#
// C# program for the above approach
 
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to find the maximum sum of
// K pairs of the form (arr[i],
// arr[N - i - 1])
static void maxSum(int []arr, int N, int K)
{
    // Stores the resultant pairwise
    // sum
    List pairwiseSum = new List();
 
    // Traverse till half of the array
    for (int i = 0; i < N / 2; i++) {
 
        // Update the value of curSum
        int curSum = arr[i] + arr[i + N / 2];
        pairwiseSum.Add(curSum);
    }
 
    // Sort in the descending order
    pairwiseSum.Sort();
    pairwiseSum.Reverse();
    // Stores the resultant maximum sum
    int maxSum = 0;
 
    // Add K maximum sums obtained
    for (int i = 0; i < K; i++) {
 
        // Update the value of maxSum
        maxSum += pairwiseSum[i];
    }
 
    // Print the maximum sum
    Console.Write(maxSum);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 2, -4, 3, 5, 2, -1 };
    int K = 2;
    int N = arr.Length;
    maxSum(arr, N, K);
 
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
9

时间复杂度: O(N * log N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程