Efficient C # program to find smallest greater element in whole array for every element .
using System ; using System . Collections . Generic ; class GFG { static void smallestGreater ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { s . Add ( arr [ i ] ) ; } int [ ] newAr = new int [ s . Count ] ; int j = 0 ; foreach ( int p in s ) { newAr [ j ] = p ; j ++ ; } Array . Sort ( newAr ) ; for ( int i = 0 ; i < n ; i ++ ) { int temp = lowerBound ( newAr , 0 , newAr . GetLength ( 0 ) , arr [ i ] ) ; if ( temp < n ) Console . Write ( newAr [ temp ] + " ▁ " ) ; else Console . Write ( " _ ▁ " ) ; } }