Domanda di colloquio di Tripadvisor

a tipical leetcode question. Sort the array with some other conditions