AC

O(n) time