Find smallest permutation under constraints | Airbnb Interview Question