Improved solution of Manasa and Stones from O(nlogn) to O(n)