Can anyone tell me the optimised solution approach ? I can get the brute force but clueless about optimised approach.