Compute minimum passes over permutation | Amazon Coding Question