Improve C algorithm of Problem 1