Find Sum of pair from two arrays with maximum sum
Given two arrays of positive and distinct integers. The task is to find a pair from the two arrays with maximum sum.
Note: The pair should contain one element from both the arrays.
Examples:
Input : arr1[] = {1, 2, 3}, arr2[] = {4, 5, 6}
Output : Max Sum = 9
Pair (3, 6) has the maximum sum.
Input : arr1[] = {10, 2, 3}, arr2[] = {3, 4, 7}
Output : Max Sum = 17
Naive Approach: A simple approach is two-run two nested loops to generate all possible pairs and find the pair with maximum sum.
Time Complexity: O(N*M), where N is the length of first array and M is the length of second array.
Efficient Approach: An efficient approach is to observe that the elements which are maximum in the respective arrays will contribute to the maximum sum pair. So, the task reduces to find maximum elements from both the arrays and print their sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSumPair( int arr1[], int n1, int arr2[], int n2)
{
int max1 = INT_MIN;
int max2 = INT_MIN;
for ( int i = 0; i < n1; i++) {
if (arr1[i] > max1)
max1 = arr1[i];
}
for ( int i = 0; i < n2; i++) {
if (arr2[i] > max2)
max2 = arr2[i];
}
return max1 + max2;
}
int main()
{
int arr1[] = { 10, 2, 3 };
int arr2[] = { 3, 4, 7 };
int n1 = sizeof (arr1) / sizeof (arr1[0]);
int n2 = sizeof (arr2) / sizeof (arr2[0]);
cout << maxSumPair(arr1, n1, arr2, n2);
return 0;
}
|
Java
public class AAB {
static int maxSumPair( int arr1[], int n1, int arr2[], int n2)
{
int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
for ( int i = 0 ; i < n1; i++) {
if (arr1[i] > max1)
max1 = arr1[i];
}
for ( int i = 0 ; i < n2; i++) {
if (arr2[i] > max2)
max2 = arr2[i];
}
return max1 + max2;
}
public static void main(String[] args) {
int arr1[] = { 10 , 2 , 3 };
int arr2[] = { 3 , 4 , 7 };
int n1 = arr1.length;
int n2 = arr2.length;
System.out.println(maxSumPair(arr1, n1, arr2, n2));
}
}
|
Python3
import sys
def maxSumPair(arr1, n1, arr2, n2):
max1 = - sys.maxsize - 1
max2 = - sys.maxsize - 1
for i in range ( 0 , n1):
if (arr1[i] > max1):
max1 = arr1[i]
for i in range ( 0 , n2):
if (arr2[i] > max2):
max2 = arr2[i]
return max1 + max2
arr1 = [ 10 , 2 , 3 ]
arr2 = [ 3 , 4 , 7 ]
n1 = len (arr1)
n2 = len (arr2)
print (maxSumPair(arr1, n1, arr2, n2))
|
C#
using System;
public class AAB {
static int maxSumPair( int []arr1, int n1, int []arr2, int n2)
{
int max1 = int .MinValue;
int max2 = int .MinValue;
for ( int i = 0; i < n1; i++) {
if (arr1[i] > max1)
max1 = arr1[i];
}
for ( int i = 0; i < n2; i++) {
if (arr2[i] > max2)
max2 = arr2[i];
}
return max1 + max2;
}
public static void Main() {
int []arr1 = { 10, 2, 3 };
int []arr2 = { 3, 4, 7 };
int n1 = arr1.Length;
int n2 = arr2.Length;
Console.Write(maxSumPair(arr1, n1, arr2, n2));
}
}
|
PHP
<?php
function maxSumPair( $arr1 , $n1 , $arr2 , $n2 )
{
$max1 = -1;
$max2 = -1;
for ( $i = 0; $i < $n1 ; $i ++)
{
if ( $arr1 [ $i ] > $max1 )
$max1 = $arr1 [ $i ];
}
for ( $i = 0; $i < $n2 ; $i ++)
{
if ( $arr2 [ $i ] > $max2 )
$max2 = $arr2 [ $i ];
}
return $max1 + $max2 ;
}
$arr1 = array ( 10, 2, 3 );
$arr2 = array ( 3, 4, 7 );
$n1 = count ( $arr1 );
$n2 = count ( $arr2 );
echo maxSumPair( $arr1 , $n1 , $arr2 , $n2 );
?>
|
Javascript
<script>
function maxSumPair(arr1,n1,arr2,n2)
{
let max1 = Number.MIN_VALUE;
let max2 = Number.MIN_VALUE;
for (let i = 0; i < n1; i++) {
if (arr1[i] > max1)
max1 = arr1[i];
}
for (let i = 0; i < n2; i++) {
if (arr2[i] > max2)
max2 = arr2[i];
}
return max1 + max2;
}
let arr1=[10, 2, 3 ];
let arr2=[3, 4, 7 ];
let n1 = arr1.length;
let n2 = arr2.length;
document.write(maxSumPair(arr1, n1, arr2, n2));
</script>
|
Time Complexity: O(N + M), where N is the length of first array and M is the length of the second array.
Last Updated :
05 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...