Maximum sum of pairs that are at least K distance apart in an array
Last Updated :
20 Jul, 2022
Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum sum of pairs of elements that are separated by at least K indices.
Examples:
Input: arr[] = {2, 4, 1, 6, 8}, K = 2
Output: 12
Explanation:
The elements {1, 4} are K(= 2) distance apart. The sum of pairs {4, 8} is 4 + 8 = 12, which is maximum.
Input: arr[] = {1, 2, 3}, K = 1
Output: 4
Naive Approach: The simplest approach to solve the given problem is to generate all possible pairs of the given array which are K distant apart and print the maximum sum among all possible pairs formed.
Time Complexity: O(N2)
Auxiliary Space: O(1), since no extra space has been taken.
Efficient Approach: The above approach can be optimized by precomputing the prefix maximums for each array element. Follow the steps below to solve the given problem:
- Initialize a variable, say res as INT_MIN, that stores the maximum sum of valid pairs which are K distant apart in the given array.
- Initialize an array, say preMax[], that stores the maximum value array element up to each index i.
- Initialize preMax[0] equal to arr[0].
- Traverse the array over the range [1, N – 1] and update the value of preMax[i] to the maximum of preMax[i – 1] and arr[i].
- Now, iterate over the range [K, N – 1], and for each index i, update the value of res as the maximum of res and (arr[i] + preMax[i – K]).
- After completing the above steps, print the value of res as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMaxPairSum( int arr[], int N,
int K)
{
int preMax[N];
preMax[0] = arr[0];
for ( int i = 1; i < N; i++) {
preMax[i] = max(preMax[i - 1],
arr[i]);
}
int res = INT_MIN;
for ( int i = K; i < N; i++) {
res = max(res, arr[i]
+ preMax[i - K]);
}
return res;
}
int main()
{
int arr[] = { 1, 2, 4, 8, 6, 3 };
int K = 3;
int N = sizeof (arr) / sizeof (arr[0]);
cout << getMaxPairSum(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static int getMaxPairSum( int [] arr, int N, int K)
{
int [] preMax = new int [N];
preMax[ 0 ] = arr[ 0 ];
for ( int i = 1 ; i < N; i++) {
preMax[i] = Math.max(preMax[i - 1 ], arr[i]);
}
int res = Integer.MIN_VALUE;
for ( int i = K; i < N; i++) {
res = Math.max(res, arr[i] + preMax[i - K]);
}
return res;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 4 , 8 , 6 , 3 };
int K = 3 ;
int N = arr.length;
System.out.print(getMaxPairSum(arr, N, K));
}
}
|
Python3
def getMaxPairSum(arr, N, K):
preMax = [ 0 ] * N
preMax[ 0 ] = arr[ 0 ]
for i in range ( 1 , N):
preMax[i] = max (preMax[i - 1 ], arr[i])
res = - 10 * * 8
for i in range (K, N):
res = max (res, arr[i] + preMax[i - K])
return res
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 8 , 6 , 3 ]
K = 3
N = len (arr)
print (getMaxPairSum(arr, N, K))
|
C#
using System;
class GFG {
static int getMaxPairSum( int [] arr, int N, int K)
{
int [] preMax = new int [N];
preMax[0] = arr[0];
for ( int i = 1; i < N; i++) {
preMax[i] = Math.Max(preMax[i - 1], arr[i]);
}
int res = Int32.MinValue;
for ( int i = K; i < N; i++)
{
res = Math.Max(res, arr[i] + preMax[i - K]);
}
return res;
}
public static void Main()
{
int [] arr = { 1, 2, 4, 8, 6, 3 };
int K = 3;
int N = arr.Length;
Console.Write(getMaxPairSum(arr, N, K));
}
}
|
Javascript
<script>
function getMaxPairSum(arr, N, K)
{
var preMax = Array(N);
preMax[0] = arr[0];
for ( var i = 1; i < N; i++) {
preMax[i] = Math.max(preMax[i - 1],
arr[i]);
}
var res = -1000000000;
for ( var i = K; i < N; i++) {
res = Math.max(res, arr[i]
+ preMax[i - K]);
}
return res;
}
var arr = [1, 2, 4, 8, 6, 3];
var K = 3;
var N = arr.length;
document.write( getMaxPairSum(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...