Ceiling of every element in same array | C # implementation of efficient algorithm to find floor of every element ; Prints greater elements on left side of every element ; Create a sorted copy of arr [ ] ; Traverse through arr [ ] and do binary search for every element . ; Find the first element that is greater than the given element ; Since arr [ i ] also exists in array , * ( it - 1 ) will be same as arr [ i ] . Let us check * ( it - 2 ) is also same as arr [ i ] . If true , then arr [ i ] exists twice in array , so ceiling is same same as arr [ i ] ; If next element is also same , then there are multiple occurrences , so print it ; Driver code
using System ; class GFG { static void printPrevGreater ( int [ ] arr , int n ) { if ( n == 1 ) { Console . Write ( " - 1" ) ; return ; } int [ ] v = new int [ arr . GetLength ( 0 ) ] ; Array . Copy ( arr , v , arr . GetLength ( 0 ) ) ; Array . Sort ( v ) ; for ( int i = 0 ; i < n ; i ++ ) { int it = Array . BinarySearch ( v , arr [ i ] ) ; it ++ ; if ( ( it - 1 ) != 0 && v [ it - 2 ] == arr [ i ] ) { Console . Write ( arr [ i ] + " ▁ " ) ; } else if ( it != v . Length ) Console . Write ( v [ it ] + " ▁ " ) ; else Console . Write ( - 1 + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 11 , 10 , 20 , 12 } ; int n = arr . Length ; printPrevGreater ( arr , n ) ; } }