Pop elements from stack while stack is not empty and top of stack is smaller than arr [ i ] . We always have elements in decreasing order in a stack .
while ( s . length != 0 && s [ s . length - 1 ] < arr [ i ] ) s . pop ( ) ;