Brute force way to solve but failed because of time in few tests.