Keep removing top element from S while the top element is smaller then or equal to arr [ i ] ( if Key is G ) element is greater then or equal to arr [ i ] ( if order is S )
while ( stack . Count != 0 && ( ( order == ' G ' ) ? arr [ stack . Peek ( ) ] <= arr [ i ] : arr [ stack . Peek ( ) ] >= arr [ i ] ) ) stack . Pop ( ) ;