If ans [ i ] is equal to - 1
if ( ans [ i ] == - 1 ) { if ( smaller . length != 0 ) { ans [ i ] = smaller [ smaller . length - 1 ] ; smaller . pop ( ) ; } else if ( greater . length != 0 ) { ans [ i ] = greater [ greater . length - 1 ] ; greater . pop ; } } }