Print all the pairs that contains the positive and negative values of an element | 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
function printPairs ( arr , n ) { let pairs = new Set ( ) ; let pair_exists = false ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] > 0 ) pairs . add ( arr [ i ] ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) if ( pairs . has ( - arr [ i ] ) ) { document . write ( arr [ i ] + " " + - arr [ i ] + " " ) ; pair_exists = true ; } } if ( pair_exists == false ) document . write ( " " ) ; } let arr = [ 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 ] ; let n = arr . length ; printPairs ( arr , n ) ;