# Find Sum of pair from two arrays with maximum sum in C++

In this problem, we are given two arrays, positive and distinct. Our task is to find the sum of pairs from two arrays with maximum sum.

We will find the pair with the maximum sum with one element from each array.

Let's take an example to understand the problem,

Input : arr1[] = {3, 7, 5}, arr2[] = {8, 2, 4}
Output : 15

Explanation

Pairs is (7, 8) = 7 + 8 = 15


## Solution Approach

A simple approach to solve the problem is using loops. We will use a nested loop and find the sum of all pairs and return the pair with maximum sum. An efficient approach to solve the problem is finding the maximum element of each array. And then find the maximum pair sum. This will use a simple loop instead of a nested loop.

## Example

Program to illustrate the working of our solution

#include <iostream>
using namespace std;
int findMaxPairSum(int arr1[], int n1, int arr2[], int n2) {
int max1 = -1;
int max2 = -1;
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[] = { 3, 7, 5 };
int arr2[] = { 8, 2, 4 };
int n1 = sizeof(arr1) / sizeof(arr1);
int n2 = sizeof(arr2) / sizeof(arr2);
cout<<"Sum of pair from two arrays with maximum sum is "<<findMaxPairSum(arr1, n1, arr2, n2);
return 0;
}

## Output

Sum of pair from two arrays with maximum sum is 15


One more approach is sorting the array. And then finding the maximum value element for both arrays and then find the maximum pair sum.

## Example

Program to illustrate the working of our solution

#include <bits/stdc++.h>
using namespace std;
int findMaxPairSum(int arr1[], int n1, int arr2[], int n2) {
sort(arr1, arr1 + n1);
sort(arr2, arr2 + n2);
return (arr1[n1 - 1] + arr2[n2 - 1]);
}
int main() {
int arr1[] = { 3, 7, 5 };
int arr2[] = { 8, 2, 4 };
int n1 = sizeof(arr1) / sizeof(arr1);
int n2 = sizeof(arr2) / sizeof(arr2);
cout<<"Sum of pair from two arrays with maximum sum is "<<findMaxPairSum(arr1, n1, arr2, n2);
return 0;
}

## Output

Sum of pair from two arrays with maximum sum is 15