I was solving this algorithm and get stuck with the pairs part.
This explanation help me a lot https://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/
So to calculate the sum of series of numbers:
n(n+1)/2
But you need to calculate this
1 + 2 + ... + (n-1)
So in order to get this you can use
n(n+1)/2 - n
that is equal to
n(n-1)/2