Floor 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 . ; Floor of first element is - 1 if there is only one occurrence of it . ; Find the first element that is greater than or or equal to given element ; If next element is also same , then there are multiple occurrences , so print it ; Otherwise print previous element ; Driver program to test insertion sort
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPrevGreater ( int arr [ ] , int n ) { vector < int > v ( arr , arr + n ) ; sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == v [ 0 ] ) { ( arr [ i ] == v [ 1 ] ) ? cout << arr [ i ] : cout << -1 ; cout << " ▁ " ; continue ; } auto it = lower_bound ( v . begin ( ) , v . end ( ) , arr [ i ] ) ; if ( it != v . end ( ) && * ( it + 1 ) == arr [ i ] ) cout << arr [ i ] << " ▁ " ; else cout << * ( it - 1 ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 6 , 11 , 7 , 8 , 20 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPrevGreater ( arr , n ) ; return 0 ; }