- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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[0]); int n2 = sizeof(arr2) / sizeof(arr2[0]); 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[0]); int n2 = sizeof(arr2) / sizeof(arr2[0]); 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

- Related Articles
- Maximum Sum of Products of Two Arrays in C++
- Maximum Sum Path in Two Arrays in C++
- Maximum OR sum of sub-arrays of two different arrays in C++
- Find a pair of elements swapping which makes sum of two arrays same in C++
- Count Pairs from two arrays with even sum in C++
- Maximum sum by picking elements from two arrays in order in C++ Program
- Find the Pair with a Maximum Sum in a Matrix using C++
- Maximum sum of increasing order elements from n arrays in C++
- Maximum sum of increasing order elements from n arrays in C++ program
- Find the overlapping sum of two arrays using C++
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
- Find the closest pair from two sorted arrays in c++
- Find a pair with given sum in BST in C++
- Find required sum pair with JavaScript
- Achieving maximum possible pair sum in JavaScript