Solve frequency sort and all-pairs sum | Amazon Interview Question