Working code for minmaxStack