Costly Array

Oyler received a peculiar homework from his school. The teacher gave him a positive number nnn and told him to construct an array of length nnn, whose entries are in the range 1,⋯ ,n1, \cdots , n1,⋯,n. The teacher also gave some extra conditions in the form of "lll rrr", meaning that the numbers at indices between lll and rrr (inclusive) should be distinct.

This is a companion discussion topic for the original entry at