Maximum sum of pairs with specific difference
Last Updated :
06 Jul, 2022
Given an array of integers and a number k. We can pair two numbers of the array if the difference between them is strictly less than k. The task is to find the maximum possible sum of disjoint pairs. Sum of P pairs is the sum of all 2P numbers of pairs.
Examples:
Input : arr[] = {3, 5, 10, 15, 17, 12, 9}, K = 4
Output : 62
Explanation:
Then disjoint pairs with difference less than K are, (3, 5), (10, 12), (15, 17)
So maximum sum which we can get is 3 + 5 + 12 + 10 + 15 + 17 = 62
Note that an alternate way to form disjoint pairs is, (3, 5), (9, 12), (15, 17), but this pairing produces lesser sum.
Input : arr[] = {5, 15, 10, 300}, k = 12
Output : 25
Approach: First, we sort the given array in increasing order. Once array is sorted, we traverse the array. For every element, we try to pair it with its previous element first. Why do we prefer previous element? Let arr[i] can be paired with arr[i-1] and arr[i-2] (i.e. arr[i] – arr[i-1] < K and arr[i]-arr[i-2] < K). Since the array is sorted, value of arr[i-1] would be more than arr[i-2]. Also, we need to pair with difference less than k, it means if arr[i-2] can be paired, then arr[i-1] can also be paired in a sorted array.
Now observing the above facts, we can formulate our dynamic programming solution as below,
Let dp[i] denotes the maximum disjoint pair sum we can achieve using first i elements of the array. Assume currently, we are at i’th position, then there are two possibilities for us.
Pair up i with (i-1)th element, i.e.
dp[i] = dp[i-2] + arr[i] + arr[i-1]
Don't pair up, i.e.
dp[i] = dp[i-1]
Above iteration takes O(N) time and sorting of array will take O(N log N) time so total time complexity of the solution will be O(N log N)
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSumPairWithDifferenceLessThanK( int arr[], int N, int K)
{
sort(arr, arr+N);
int dp[N];
dp[0] = 0;
for ( int i = 1; i < N; i++)
{
dp[i] = dp[i-1];
if (arr[i] - arr[i-1] < K)
{
if (i >= 2)
dp[i] = max(dp[i], dp[i-2] + arr[i] + arr[i-1]);
else
dp[i] = max(dp[i], arr[i] + arr[i-1]);
}
}
return dp[N - 1];
}
int main()
{
int arr[] = {3, 5, 10, 15, 17, 12, 9};
int N = sizeof (arr)/ sizeof ( int );
int K = 4;
cout << maxSumPairWithDifferenceLessThanK(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int maxSumPairWithDifferenceLessThanK( int arr[],
int N, int K)
{
Arrays.sort(arr);
int dp[] = new int [N];
dp[ 0 ] = 0 ;
for ( int i = 1 ; i < N; i++)
{
dp[i] = dp[i- 1 ];
if (arr[i] - arr[i- 1 ] < K)
{
if (i >= 2 )
dp[i] = Math.max(dp[i], dp[i- 2 ] + arr[i] +
arr[i- 1 ]);
else
dp[i] = Math.max(dp[i], arr[i] + arr[i- 1 ]);
}
}
return dp[N - 1 ];
}
public static void main (String[] args) {
int arr[] = { 3 , 5 , 10 , 15 , 17 , 12 , 9 };
int N = arr.length;
int K = 4 ;
System.out.println ( maxSumPairWithDifferenceLessThanK(
arr, N, K));
}
}
|
Python3
def maxSumPairWithDifferenceLessThanK(arr, N, K):
arr.sort()
dp = [ 0 ] * N
dp[ 0 ] = 0
for i in range ( 1 , N):
dp[i] = dp[i - 1 ]
if (arr[i] - arr[i - 1 ] < K):
if (i > = 2 ):
dp[i] = max (dp[i], dp[i - 2 ] + arr[i] + arr[i - 1 ]);
else :
dp[i] = max (dp[i], arr[i] + arr[i - 1 ]);
return dp[N - 1 ]
arr = [ 3 , 5 , 10 , 15 , 17 , 12 , 9 ]
N = len (arr)
K = 4
print (maxSumPairWithDifferenceLessThanK(arr, N, K))
|
C#
using System;
class GFG {
static int maxSumPairWithDifferenceLessThanK( int []arr,
int N, int K)
{
Array.Sort(arr);
int []dp = new int [N];
dp[0] = 0;
for ( int i = 1; i < N; i++)
{
dp[i] = dp[i-1];
if (arr[i] - arr[i-1] < K)
{
if (i >= 2)
dp[i] = Math.Max(dp[i], dp[i-2]
+ arr[i] + arr[i-1]);
else
dp[i] = Math.Max(dp[i], arr[i]
+ arr[i-1]);
}
}
return dp[N - 1];
}
public static void Main () {
int []arr = {3, 5, 10, 15, 17, 12, 9};
int N = arr.Length;
int K = 4;
Console.WriteLine(
maxSumPairWithDifferenceLessThanK(arr, N, K));
}
}
|
PHP
<?php
function maxSumPairWithDifferenceLessThanK( $arr , $N , $K )
{
sort( $arr ) ;
$dp = array () ;
$dp [0] = 0;
for ( $i = 1; $i < $N ; $i ++)
{
$dp [ $i ] = $dp [ $i -1];
if ( $arr [ $i ] - $arr [ $i -1] < $K )
{
if ( $i >= 2)
$dp [ $i ] = max( $dp [ $i ], $dp [ $i -2] + $arr [ $i ] + $arr [ $i -1]);
else
$dp [ $i ] = max( $dp [ $i ], $arr [ $i ] + $arr [ $i -1]);
}
}
return $dp [ $N - 1];
}
$arr = array (3, 5, 10, 15, 17, 12, 9);
$N = sizeof( $arr ) ;
$K = 4;
echo maxSumPairWithDifferenceLessThanK( $arr , $N , $K );
?>
|
Javascript
<script>
function maxSumPairWithDifferenceLessThanK(arr,
N, K)
{
arr.sort();
let dp = [];
dp[0] = 0;
for (let i = 1; i < N; i++)
{
dp[i] = dp[i-1];
if (arr[i] - arr[i-1] < K)
{
if (i >= 2)
dp[i] = Math.max(dp[i], dp[i-2] + arr[i] +
arr[i-1]);
else
dp[i] = Math.max(dp[i], arr[i] + arr[i-1]);
}
}
return dp[N - 1];
}
let arr = [3, 5, 10, 15, 17, 12, 9];
let N = arr.length;
let K = 4;
document.write( maxSumPairWithDifferenceLessThanK(
arr, N, K));
</script>
|
Time complexity: O(N Log N)
Auxiliary Space: O(N)
An optimised solution contributed by Amit Sane is given below,
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSumPair( int arr[], int N, int k)
{
int maxSum = 0;
sort(arr, arr + N);
for ( int i = N - 1; i > 0; --i)
{
if (arr[i] - arr[i - 1] < k)
{
maxSum += arr[i];
maxSum += arr[i - 1];
--i;
}
}
return maxSum;
}
int main()
{
int arr[] = { 3, 5, 10, 15, 17, 12, 9 };
int N = sizeof (arr) / sizeof ( int );
int K = 4;
cout << maxSumPair(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int maxSumPairWithDifferenceLessThanK( int arr[],
int N,
int k)
{
int maxSum = 0 ;
Arrays.sort(arr);
for ( int i = N - 1 ; i > 0 ; --i)
{
if (arr[i] - arr[i - 1 ] < k)
{
maxSum += arr[i];
maxSum += arr[i - 1 ];
--i;
}
}
return maxSum;
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 , 10 , 15 , 17 , 12 , 9 };
int N = arr.length;
int K = 4 ;
System.out.println(
maxSumPairWithDifferenceLessThanK(arr, N, K));
}
}
|
Python3
def maxSumPairWithDifferenceLessThanK(arr, N, k):
maxSum = 0
arr.sort()
i = N - 1
while (i > 0 ):
if (arr[i] - arr[i - 1 ] < k):
maxSum + = arr[i]
maxSum + = arr[i - 1 ]
i - = 1
i - = 1
return maxSum
arr = [ 3 , 5 , 10 , 15 , 17 , 12 , 9 ]
N = len (arr)
K = 4
print (maxSumPairWithDifferenceLessThanK(arr, N, K))
|
C#
using System;
class GFG {
static int maxSumPairWithDifferenceLessThanK( int []arr,
int N, int k)
{
int maxSum = 0;
Array.Sort(arr);
for ( int i = N-1; i > 0; --i)
{
if (arr[i] - arr[i-1] < k)
{
maxSum += arr[i];
maxSum += arr[i - 1];
--i;
}
}
return maxSum;
}
public static void Main ()
{
int []arr = {3, 5, 10, 15, 17, 12, 9};
int N = arr.Length;
int K = 4;
Console.Write( maxSumPairWithDifferenceLessThanK(arr,
N, K));
}
}
|
PHP
<?php
function maxSumPairWithDifferenceLessThanK( $arr , $N , $k )
{
$maxSum = 0;
sort( $arr );
for ( $i = $N - 1; $i > 0; -- $i )
{
if ( $arr [ $i ] - $arr [ $i - 1] < $k )
{
$maxSum += $arr [ $i ];
$maxSum += $arr [ $i - 1];
-- $i ;
}
}
return $maxSum ;
}
$arr = array (3, 5, 10, 15, 17, 12, 9);
$N = sizeof( $arr );
$K = 4;
echo maxSumPairWithDifferenceLessThanK( $arr , $N , $K );
?>
|
Javascript
<script>
function maxSumPairWithDifferenceLessThanK(arr, N, k)
{
var maxSum = 0;
arr.sort((a,b)=>a-b);
for (i = N - 1; i > 0; --i)
{
if (arr[i] - arr[i - 1] < k)
{
maxSum += arr[i];
maxSum += arr[i - 1];
--i;
}
}
return maxSum;
}
var arr = [ 3, 5, 10, 15, 17, 12, 9 ];
var N = arr.length;
var K = 4;
document.write(maxSumPairWithDifferenceLessThanK(arr, N, K));
</script>
|
Time complexity: O(N Log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...