interesting one, solved in O(N) time with constant space