Adding Akshay's algorithm.