Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | Function to print pairs ; Initializing the rem values with 0 's. ; Perform the remainder operation only if the element is x , as numbers greater than x can 't  be used to get a sum x.  Updating the count of remainders. ; Traversing the remainder list from start to middle to find pairs ; The elements with remainders i and x - i will result to a sum of x . Once we get two elements which add up to x , we print x and break . ; Once we reach middle of remainder array , we have to do operations based on x . ; If x is even and we have more than 1 elements with remainder x / 2 , then we will have two distinct elements which add up to x . if we dont have more than 1 element , print " No " . ; When x is odd we continue the same process which we did in previous loop . ; Driver code ; Function calling
function printPairs ( a , n , x ) { let i ; let rem = new Array ( x ) ; for ( i = 0 ; i < x ; i ++ ) { rem [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] < x ) { rem [ a [ i ] % x ] ++ ; } } for ( i = 1 ; i < x / 2 ; i ++ ) { if ( rem [ i ] > 0 && rem [ x - i ] > 0 ) { document . write ( " " + " " ) ; break ; } } if ( i >= x / 2 ) { if ( x % 2 == 0 ) { if ( rem [ x / 2 ] > 1 ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; } } else { if ( rem [ x / 2 ] > 0 && rem [ x - x / 2 ] > 0 ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; } } } } let A = [ 1 , 4 , 45 , 6 , 10 , 8 ] ; let n = 16 ; let arr_size = A . length ; printPairs ( A , arr_size , n ) ;