Ceiling of every element in same array | Java 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
import java . util . Arrays ; class GFG { static void printPrevGreater ( int arr [ ] , int n ) { if ( n == 1 ) { System . out . println ( " - 1" ) ; return ; } int v [ ] = Arrays . copyOf ( arr , arr . length ) ; Arrays . sort ( v ) ; for ( int i = 0 ; i < n ; i ++ ) { int it = Arrays . binarySearch ( v , arr [ i ] ) ; it ++ ; if ( ( it - 1 ) != 0 && v [ it - 2 ] == arr [ i ] ) { System . out . print ( arr [ i ] + " ▁ " ) ; } else if ( it != v . length ) System . out . print ( v [ it ] + " ▁ " ) ; else System . out . print ( - 1 + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 5 , 11 , 10 , 20 , 12 } ; int n = arr . length ; printPrevGreater ( arr , n ) ; } }