Is there a way to solve this problem in faster than linear time? I came up with the deque solution but they wanted something faster.