Maximize sum of K pairs made up of elements that are equidistant from both ends of the array
Last Updated :
19 Jul, 2021
Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum sum of K pairs of the form (arr[i], arr[N – i – 1]), where (0 ? i ? N – 1).
Examples:
Input: arr[] = {2, -4, 3, -1, 2, 5}, K = 2
Output: 9
Explanation: All possibles pair of the form (arr[i], arr[N – i + 1]) are:
- (2, 5)
- (-1, 3)
- (-4, 2)
From the above pairs, the K(= 2) pairs with maximum sum are (2, 5) and (-1, 3). Therefore, maximum sum = 2 + 5 + (-1) + 3 = 9.
Input: arr[] = {2, -4, -2, 4}, K = 2
Output: 0
Naive Approach: The simplest approach to solve the problem is to generate all possible pairs of the given form from the array and calculate the maximum sum of K such pairs. After checking for all the pairs, print the maximum sum obtained among all the possible sums.
Time Complexity: O(N2 * K)
Auxiliary Space: O(N2)
Efficient Approach: The above approach can be optimized greedily. The idea is to choose only the K pairs with highest cost. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum( int arr[], int N, int K)
{
vector< int > pairwiseSum;
for ( int i = 0; i < N / 2; i++) {
int curSum = arr[i] + arr[i + N / 2];
pairwiseSum.push_back(curSum);
}
sort(pairwiseSum.begin(),
pairwiseSum.end(),
greater< int >());
int maxSum = 0;
for ( int i = 0; i < K; i++) {
maxSum += pairwiseSum[i];
}
cout << maxSum;
}
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
import java.util.ArrayList;
import java.util.Collections;
class GFG{
public static void maxSum( int arr[], int N, int K)
{
ArrayList<Integer> pairwiseSum = new ArrayList<Integer>();
for ( int i = 0 ; i < N / 2 ; i++) {
int curSum = arr[i] + arr[i + N / 2 ];
pairwiseSum.add(curSum);
}
Collections.sort(pairwiseSum);
Collections.reverse(pairwiseSum);
int maxSum = 0 ;
for ( int i = 0 ; i < K; i++) {
maxSum += pairwiseSum.get(i);
}
System.out.println(maxSum);
}
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);
}
}
|
Python3
def maxSum(arr, N, K):
pairwiseSum = []
for i in range (N / / 2 ):
curSum = arr[i] + arr[i + N / / 2 ]
pairwiseSum.append(curSum)
pairwiseSum.sort(reverse = True )
maxSum = 0
for i in range (K):
maxSum + = pairwiseSum[i]
print (maxSum)
if __name__ = = '__main__' :
arr = [ 2 , - 4 , 3 , 5 , 2 , - 1 ]
K = 2
N = len (arr)
maxSum(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void maxSum( int []arr, int N, int K)
{
List< int > pairwiseSum = new List< int >();
for ( int i = 0; i < N / 2; i++) {
int curSum = arr[i] + arr[i + N / 2];
pairwiseSum.Add(curSum);
}
pairwiseSum.Sort();
pairwiseSum.Reverse();
int maxSum = 0;
for ( int i = 0; i < K; i++) {
maxSum += pairwiseSum[i];
}
Console.Write(maxSum);
}
public static void Main()
{
int []arr = { 2, -4, 3, 5, 2, -1 };
int K = 2;
int N = arr.Length;
maxSum(arr, N, K);
}
}
|
Javascript
<script>
function maxSum(arr, N, K) {
let pairwiseSum = [];
for (let i = 0; i < N / 2; i++) {
let curSum = arr[i] + arr[i + parseInt(N / 2)];
pairwiseSum.push(curSum);
}
pairwiseSum.sort( function (a, b) { return b - a });
let maxSum = 0;
for (let i = 0; i < K; i++) {
maxSum += pairwiseSum[i];
}
document.write(maxSum);
}
let arr = [2, -4, 3, 5, 2, -1];
let K = 2;
let N = arr.length;
maxSum(arr, N, K);
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...