Duplicating an iterator is quite expensive, it basically requires copying the whole thing to a buffer. It would be better to design a sliding function that computes both the first n and last n.