Find two non | C ++ programs to find two non - overlapping pairs having equal sum in an Array ; Function to find two non - overlapping with same sum in an array ; first create an empty map key -> which is sum of a pair of elements in the array value -> vector storing index of every pair having that sum ; consider every pair ( arr [ i ] , arr [ j ] ) and where ( j > i ) ; calculate sum of current pair ; if sum is already present in the map ; check every pair having equal sum ; if pairs don 't overlap,  print them and return ; Insert current pair into the map ; If no such pair found ; Driver Code
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE #include <vector> NEW_LINE using namespace std ; typedef pair < int , int > Pair ; void findPairs ( int arr [ ] , int n ) { unordered_map < int , vector < Pair > > map ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( map . find ( sum ) != map . end ( ) ) { for ( auto pair : map . find ( sum ) -> second ) { int m = pair . first , n = pair . second ; if ( ( m != i && m != j ) && ( n != i && n != j ) ) { cout << " Pair ▁ First ( " << arr [ i ] << " , ▁ " << arr [ j ] << " Pair Second ( " ▁ < < ▁ arr [ m ] ▁ < < ▁ " , " ▁ < < ▁ arr [ n ] ▁ < < ▁ " ) " return ; } } } map [ sum ] . push_back ( { i , j } ) ; } } cout << " No ▁ such ▁ non - overlapping ▁ pairs ▁ present " ; } int main ( ) { int arr [ ] = { 8 , 4 , 7 , 8 , 4 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPairs ( arr , size ) ; return 0 ; }