Thinking Through Ranges and Avoiding Fencepost Errors
Because this post needed lots of diagrams, sections, and boxes, I did something unusual and hand-wrote it on graph paper and scanned it to a PDF. To continue reading, please click the link below.
Beware: you'll have to do some thinking! This is a really common and surprisingly difficult problem, though, so it's worth trying to understand.