Sort first, then look at neighbours
transcript
Which two orders came closest together? Checking every pair is slow. Sort first. Then look only at neighbours.
A hundred thousand orders make five billion pairs. Sorting needs about one point six million comparisons.
A value between two others is never farther from one of them. So only neighbours can be the closest pair. One pass answers the question.
Times: seven, twelve, twenty-five, thirty-one, forty. Seven and twelve are five apart. The next gaps are thirteen, six and nine. The smallest is five.
Without sorting, neighbours are only arrival order. The best unsorted gap here is thirteen. Sorted, it is five.
Test three edge cases before you submit. Equal times give a gap of zero. And a clock wraps at midnight.
Before you code, write what the scan reads. Then write which way the sort goes.
Sort so that the answer is local. Then scan neighbours, or positions. Full breakdown in the lecture.