one of the best solution using sets and algo