
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print All Sum Pairs Occurring Maximum Times in C++
In this problem, we are given an array of n unique integers. And we have to find the sum of two integers of the array which has the maximum frequency. The problem has multiple solutions and you need to find them all.
Input : array = { 1, 12, 5, 7, 9, 11} Output : 16 12
Explanation − sum 16 and 12 occur two times.
5 + 11 = 16 & 7 + 9 = 16 1 + 11 = 12 & 5 + 7 = 12
Now to solve this problem, our approach to the problem is checking the occurrence every sum pair and then print the pair with the maximum number of times.
Steps to solve the problem −
Step 1: Iterate over all pairs. Step 2: The occurrence of sum pairs is counted using hash-table. Step 3: After the interation process is done, the sum pair with maximum occurrence is printed.
Example
#includeusing namespace std; void sumPairs(int a[], int n){ unordered_map pairSum; for (int i = 0; i occur) { occur = it.second; } } for (auto it : pairSum) { if (it.second == occur) cout Output
The sum pairs with max occurrence are −
16 12
Advertisements