https://community.topcoder.com/stat?c=problem_statement&pm=2986&rd=5862
How to solve this in O(sqrt(n))