Problem 249
Speedup by using array