Print all the pairs that contains the positive and negative values of an element | CPP program to find pairs of positive and negative values present in an array ; Function to print pairs of positive and negative values present in the array ; Store all the positive elements in the unordered_set ; Start traversing the array ; Check if the positive value of current element exists in the set or not ; { Print that pair ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPairs ( int arr [ ] , int n ) { unordered_set < int > pairs ; bool pair_exists = false ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] > 0 ) pairs . insert ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) if ( pairs . find ( - arr [ i ] ) != pairs . end ( ) ) cout << arr [ i ] << " , ▁ " << - arr [ i ] << endl ; pair_exists = true ; } } if ( pair_exists == false ) cout << " No ▁ such ▁ pair ▁ exists " ; } int main ( ) { int arr [ ] = { 4 , 8 , 9 , -4 , 1 , -1 , -8 , -9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPairs ( arr , n ) ; return 0 ; }