Doubt in the question

In this question how are we going to take the input because we have no idea when to stop???
Array list can be used but how ???