Efficient Javascript program to find smallest greater element in whole array for every element .
function smallestGreater ( arr , n ) { let s = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) s . add ( arr [ i ] ) ; let newAr = [ ] ; for ( let p of s . values ( ) ) { newAr . push ( p ) ; } newAr . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) { let temp = lowerBound ( newAr , 0 , newAr . length , arr [ i ] ) ; if ( temp < n ) document . write ( newAr [ temp ] + " " ) ; else document . write ( " " ) ; } }