Find Sum of pair from two arrays with maximum sum
Last Updated :
05 Sep, 2022
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++
// C++ program to find maximum sum
// pair from two arrays
#include <bits/stdc++.h>
using namespace std;
// Function to find maximum sum
// pair from two arrays
int maxSumPair(int arr1[], int n1, int arr2[], int n2)
{
int max1 = INT_MIN; // max from 1st array
int max2 = INT_MIN; // max from 2nd array
// Find max from 1st array
for (int i = 0; i < n1; i++) {
if (arr1[i] > max1)
max1 = arr1[i];
}
// Find max from 2nd array
for (int i = 0; i < n2; i++) {
if (arr2[i] > max2)
max2 = arr2[i];
}
// Return sum of max from both arrays
return max1 + max2;
}
// Driver Code
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
//Java program to find maximum sum
//pair from two arrays
public class AAB {
//Function to find maximum sum
//pair from two arrays
static int maxSumPair(int arr1[], int n1, int arr2[], int n2)
{
int max1 = Integer.MIN_VALUE; // max from 1st array
int max2 = Integer.MIN_VALUE; // max from 2nd array
// Find max from 1st array
for (int i = 0; i < n1; i++) {
if (arr1[i] > max1)
max1 = arr1[i];
}
// Find max from 2nd array
for (int i = 0; i < n2; i++) {
if (arr2[i] > max2)
max2 = arr2[i];
}
// Return sum of max from both arrays
return max1 + max2;
}
//Driver Code
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
# Python3 program to find maximum
# sum pair from two arrays
import sys
# Function to find maximum sum
# pair from two arrays
def maxSumPair(arr1, n1, arr2, n2):
max1 = -sys.maxsize -1 # max from 1st array
max2 = -sys.maxsize -1 # max from 2nd array
# Find max from 1st array
for i in range(0, n1):
if (arr1[i] > max1):
max1 = arr1[i]
# Find max from 2nd array
for i in range(0, n2):
if (arr2[i] > max2):
max2 = arr2[i]
# Return sum of max from
# both arrays
return max1 + max2
# Driver Code
arr1 = [ 10, 2, 3 ]
arr2 = [ 3, 4, 7 ]
n1 = len(arr1)
n2 = len(arr2)
print(maxSumPair(arr1, n1, arr2, n2))
# This code is contributed
# by Yatin Gupta
C#
// C# program to find maximum sum
//pair from two arrays
using System;
public class AAB {
//Function to find maximum sum
//pair from two arrays
static int maxSumPair(int []arr1, int n1, int []arr2, int n2)
{
int max1 = int.MinValue; // max from 1st array
int max2 = int.MinValue; // max from 2nd array
// Find max from 1st array
for (int i = 0; i < n1; i++) {
if (arr1[i] > max1)
max1 = arr1[i];
}
// Find max from 2nd array
for (int i = 0; i < n2; i++) {
if (arr2[i] > max2)
max2 = arr2[i];
}
// Return sum of max from both arrays
return max1 + max2;
}
//Driver Code
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));
}
}
/*This code is contributed by 29AjayKumar*/
PHP
<?php
// PHP program to find maximum sum
// pair from two arrays
// Function to find maximum sum
// pair from two arrays
function maxSumPair($arr1, $n1, $arr2, $n2)
{
$max1 = -1; // max from 1st array
$max2 = -1; // max from 2nd array
// Find max from 1st array
for ($i = 0; $i < $n1; $i++)
{
if ($arr1[$i] > $max1)
$max1 = $arr1[$i];
}
// Find max from 2nd array
for ($i = 0; $i < $n2; $i++)
{
if ($arr2[$i] > $max2)
$max2 = $arr2[$i];
}
// Return sum of max from both arrays
return $max1 + $max2;
}
// Driver Code
$arr1 = array( 10, 2, 3 );
$arr2 = array( 3, 4, 7 );
$n1 = count($arr1);
$n2 = count($arr2);
echo maxSumPair($arr1, $n1, $arr2, $n2);
// This code is contributed by Rajput-Ji
?>
JavaScript
<script>
// Javascript program to find maximum sum
// pair from two arrays
// Function to find maximum sum
// pair from two arrays
function maxSumPair(arr1,n1,arr2,n2)
{
// max from 1st array
let max1 = Number.MIN_VALUE;
// max from 2nd array
let max2 = Number.MIN_VALUE;
// Find max from 1st array
for (let i = 0; i < n1; i++) {
if (arr1[i] > max1)
max1 = arr1[i];
}
// Find max from 2nd array
for (let i = 0; i < n2; i++) {
if (arr2[i] > max2)
max2 = arr2[i];
}
// Return sum of max from both arrays
return max1 + max2;
}
// Driver Code
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));
// This code is contributed by rag2127
</script>
Time Complexity: O(N + M), where N is the length of first array and M is the length of the second array.
Similar Reads
Pair with maximum GCD from two arrays Given two arrays of n integers with values of the array being small (values never exceed a small number say 100). Find the pair(x, y) which has maximum gcd. x and y cannot be of the same array. If multiple pairs have the same gcd, then consider the pair which has the maximum sum. Examples: Input : a
15+ min read
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 = 30Outp
12 min read
Find k pairs with smallest sums in two arrays Given two integer arrays arr1[] and arr2[] sorted in ascending order and an integer k. Find k pairs with smallest sums such that one element of a pair belongs to arr1[] and other element belongs to arr2[] Examples: Input : arr1[] = {1, 7, 11} arr2[] = {2, 4, 6} k = 3Output : [1, 2], [1, 4], [1, 6]Ex
15+ min read
Maximum Sum Path in Two Arrays Given two sorted arrays having some elements in common. Find the sum of the maximum sum path to reach from the beginning of any array to the end of any of the two arrays. We can switch from one array to another array only at common elements. Note: The common elements do not have to be at the same in
14 min read
Find a pair with maximum product in array of Integers Given an array with both +ive and -ive integers, return a pair with the highest product. Examples : Input: arr[] = {1, 4, 3, 6, 7, 0} Output: {6,7} Input: arr[] = {-1, -3, -4, 2, 0, -5} Output: {-4,-5} Recommended PracticeMaximum product of two numbersTry It! A Simple Solution is to consider every p
15+ min read
Find a pair from the given array with maximum nCr value Given an array arr[] of n positive integers. The task is to find elements arr[i] and arr[j] from the array such that arr[i]Carr[j] is maximum possible. In case of more than 1 valid pairs, print any one of them.Examples: Input: arr[] = {3, 1, 2} Output: 3 2 3C1 = 3 3C2 = 3 2C1 = 2 (3, 1) and (3, 2) a
14 min read
Find k pairs with smallest sums in two arrays | Set 2 Given two arrays arr1[] and arr2[] sorted in ascending order and an integer K. The task is to find k pairs with the smallest sums such that one element of a pair belongs to arr1[] and another element belongs to arr2[]. The sizes of arrays may be different. Assume all the elements to be distinct in e
15+ min read
Find maximum sum pair with same digit sum Given an array arr having N integers, the task is to find a pair with maximum sum and having the same sum of digits. Print the sum of that pair, if it exists. Otherwise, print -1. Examples: Input: arr[]={55, 23, 32, 46, 88}Output: 46 55 101Explanation: Pair {55, 46} will give the sum of 55 + 46 = 10
7 min read
Maximum sum combination from two arrays Given two arrays arr1[] and arr2[] each of size N. The task is to choose some elements from both arrays such that no two elements have the same index and no two consecutive numbers can be selected from a single array. Find the maximum sum possible of the above-chosen numbers. Examples: Input : arr1[
10 min read
Pair with maximum sum in a Matrix Given a NxM matrix with N rows and M columns of positive integers. The task is to find the sum of pair with maximum sum in the matrix. Examples: Input : mat[N][M] = {{1, 2, 3, 4}, {25, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} Output : 41 Pair (25, 16) has the maximum sum Input : mat[N][M] = {{1,
7 min read