Maximum possible pair sum at most K from two Arrays
Given two arrays arr1[] and arr2[] of sizes N and M and an integer K, the task is to find the maximum possible sum pair from two arrays such that the sum is at most K.
Note: You have to take one element from each array.
Examples:
Input: arr1[] = {5, 4, 1, 2, 3}, arr2[] = {30, 20, 40, 10}, K = 30
Output: 25
Explanation: Maximum sum of a pair is (20 + 5) = 25 that is at most 30.
Input: toffee[] = {7, 6, 8}, candy[] = {9, 1, 5, 2, 8}, K = 5
Output: -1
Explanation: There is no such pair which have a pair sum less than or equal to 5
Approach: The basic idea to solve the problem is mentioned below:
Create all the possible pairs from arr1[] and arr2[]. From these pairs find a pair whose sum is the greatest and at most equal to K.
Follow the steps to solve the problem:
- Define a variable (say sum = 0)to store the pair sum.
- Traverse the array arr1[] from i = 0 to N:
- For each element of arr1[] traverse from j = 0 to M of arr2[] to form all possible pairs and:
- Calculate the pair sum of arr1[i] and arr2[j].
- If the sum is greater than the maximum pair sum till now and not greater than K, update the maximum value.
- After the loop ends, return the maximum value as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maximumAmount( int arr1[], int arr2[],
int k, int n, int m)
{
int ans = INT_MIN, sum;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
sum = arr1[i] + arr2[j];
if (sum > ans && sum <= k)
ans = sum;
}
}
return ans;
}
int main()
{
int arr1[] = { 5, 4, 1, 2, 3 };
int arr2[] = { 30, 20, 40, 10 };
int N = sizeof (arr1) / sizeof (arr1[0]);
int M = sizeof (arr2) / sizeof (arr2[0]);
int K = 30;
cout << maximumAmount(arr1, arr2, K, N, M);
return 0;
}
|
Java
import java.util.ArrayList;
class GFG {
static int maximumAmount( int arr1[], int arr2[],
int k, int n, int m)
{
int ans = Integer.MIN_VALUE, sum;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
sum = arr1[i] + arr2[j];
if (sum > ans && sum <= k)
ans = sum;
}
}
return ans;
}
public static void main(String args[]) {
int arr1[] = { 5 , 4 , 1 , 2 , 3 };
int arr2[] = { 30 , 20 , 40 , 10 };
int N = arr1.length;
int M = arr2.length;
int K = 30 ;
System.out.print(maximumAmount(arr1, arr2, K, N, M));
}
}
|
Python3
import sys
def maximumAmount(arr1, arr2,k, n, m) :
ans = - sys.maxsize;
sum = 0 ;
for i in range (n) :
for j in range (m) :
sum = arr1[i] + arr2[j];
if ( sum > ans and sum < = k) :
ans = sum ;
return ans;
if __name__ = = "__main__" :
arr1 = [ 5 , 4 , 1 , 2 , 3 ];
arr2 = [ 30 , 20 , 40 , 10 ];
N = len (arr1);
M = len (arr2);
K = 30 ;
print (maximumAmount(arr1, arr2, K, N, M));
|
C#
using System;
public class GFG {
static int maximumAmount( int []arr1, int []arr2,
int k, int n, int m)
{
int ans = int .MinValue;
int sum;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
sum = arr1[i] + arr2[j];
if (sum > ans && sum <= k)
ans = sum;
}
}
return ans;
}
public static void Main( string []arg) {
int []arr1 = { 5, 4, 1, 2, 3 };
int []arr2 = { 30, 20, 40, 10 };
int N = arr1.Length;
int M = arr2.Length;
int K = 30;
Console.WriteLine(maximumAmount(arr1, arr2, K, N, M));
}
}
|
Javascript
function maximumAmount( arr1, arr2, k, n, m)
{
let ans = Number.MIN_SAFE_INTEGER,
let sum;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
sum = arr1[i] + arr2[j];
if (sum > ans && sum <= k)
ans = sum;
}
}
return ans;
}
let arr1 = [5, 4, 1, 2, 3 ];
let arr2 = [30, 20, 40, 10 ];
let N = arr1.length;
let M = arr2.length;
let K = 30;
let ans = maximumAmount(arr1, arr2, K, N, M);
document.write(ans);
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)
New Approach:- Here an alternative approach to solve the problem of finding the maximum possible pair sum at most K from two arrays is to use sorting and two-pointer technique.
The steps to solve the problem using this approach are:
- Sort both the arrays arr1[] and arr2[] in non-decreasing order.
- Initialize two pointers i and j, pointing to the end of the arr1[] and arr2[] respectively.
- Traverse the arrays by moving the pointers i and j towards the beginning of the arrays, and calculate the pair sum of arr1[i] and arr2[j].
- If the sum is greater than K, then decrement j, else update the maximum pair sum.
- Repeat steps 3 and 4 until either i or j becomes negative.
- Return the maximum pair sum.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maximumAmount( int arr1[], int arr2[], int k, int n,
int m)
{
sort(arr1, arr1 + n);
sort(arr2, arr2 + m);
int i = n - 1, j = m - 1;
int ans = INT_MIN;
while (i >= 0 && j >= 0) {
int sum = arr1[i] + arr2[j];
if (sum <= k)
ans = max(ans, sum);
if (sum > k)
j--;
else
i--;
}
return ans;
}
int main()
{
int arr1[] = { 5, 4, 1, 2, 3 };
int arr2[] = { 30, 20, 40, 10 };
int N = sizeof (arr1) / sizeof (arr1[0]);
int M = sizeof (arr2) / sizeof (arr2[0]);
int K = 30;
cout << maximumAmount(arr1, arr2, K, N, M);
}
|
Java
import java.util.Arrays;
class GFG {
static int maximumAmount( int arr1[], int arr2[], int k,
int n, int m)
{
Arrays.sort(arr1);
Arrays.sort(arr2);
int i = n - 1 , j = m - 1 ;
int ans = Integer.MIN_VALUE;
while (i >= 0 && j >= 0 ) {
int sum = arr1[i] + arr2[j];
if (sum <= k)
ans = Math.max(ans, sum);
if (sum > k)
j--;
else
i--;
}
return ans;
}
public static void main(String args[])
{
int arr1[] = { 5 , 4 , 1 , 2 , 3 };
int arr2[] = { 30 , 20 , 40 , 10 };
int N = arr1.length;
int M = arr2.length;
int K = 30 ;
System.out.print(
maximumAmount(arr1, arr2, K, N, M));
}
}
|
Python3
def maximumAmount(arr1, arr2, k, n, m):
arr1.sort()
arr2.sort()
i, j = n - 1 , m - 1
ans = float ( '-inf' )
while i > = 0 and j > = 0 :
pair_sum = arr1[i] + arr2[j]
if pair_sum < = k:
ans = max (ans, pair_sum)
if pair_sum > k:
j - = 1
else :
i - = 1
return ans
arr1 = [ 5 , 4 , 1 , 2 , 3 ]
arr2 = [ 30 , 20 , 40 , 10 ]
N = len (arr1)
M = len (arr2)
K = 30
print (maximumAmount(arr1, arr2, K, N, M))
|
C#
using System;
public class Program {
public static int MaximumAmount( int [] arr1, int [] arr2,
int k, int n, int m)
{
Array.Sort(arr1);
Array.Sort(arr2);
int i = n - 1, j = m - 1;
int ans = int .MinValue;
while (i >= 0 && j >= 0) {
int pairSum = arr1[i] + arr2[j];
if (pairSum <= k) {
ans = Math.Max(ans, pairSum);
}
if (pairSum > k) {
j--;
}
else {
i--;
}
}
return ans;
}
public static void Main()
{
int [] arr1 = { 5, 4, 1, 2, 3 };
int [] arr2 = { 30, 20, 40, 10 };
int n = arr1.Length;
int m = arr2.Length;
int k = 30;
Console.WriteLine(
MaximumAmount(arr1, arr2, k, n, m));
}
}
|
Javascript
function maximumAmount(arr1, arr2, k) {
arr1.sort((a, b) => a - b);
arr2.sort((a, b) => a - b);
let i = arr1.length - 1;
let j = arr2.length - 1;
let ans = Number.MIN_SAFE_INTEGER;
while (i >= 0 && j >= 0) {
let sum = arr1[i] + arr2[j];
if (sum <= k) {
ans = Math.max(ans, sum);
}
if (sum > k) {
j--;
}
else {
i--;
}
}
return ans;
}
const arr1 = [5, 4, 1, 2, 3];
const arr2 = [30, 20, 40, 10];
const K = 30;
console.log(maximumAmount(arr1, arr2, K));
|
Output:-
25
Time Complexity: The time complexity of the given solution is O(N log N + M log M), where N and M are the sizes of the input arrays arr1 and arr2, respectively. The sorting of the arrays takes O(N log N + M log M) time, and the while loop takes O(N + M) time. Since O(N + M) is dominated by O(N log N + M log M), the overall time complexity is O(N log N + M log M).
Auxiliary Space: The auxiliary space used by the solution is O(1), as we are not using any additional data structures and are only using a constant amount of memory to store the pointers and the maximum pair sum.
Last Updated :
29 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...