Does it work if there are 2 people in the room?

2 years ago
10.2K

Let's see.Does it work if there are 2 people in the room?In line 1, we initialize n to zero.For that one pair of people, we then increment n by 2.And so, by this algorithm's end, n is 2,which indeed matches the number of people in the room.Suppose next that there are zero people in the room.In line 1, we initialize n to zero.As before, line 3 doesn't execute at all since there aren't any pairs of people in the room,and so, n remains zero,which indeed matches the number of people in the room.

Loading comments...