Solved problem 1 in constant time