how to calculate the number of less comparisons in this algorithm I have this algorithm

The teacher asked us what is the number of the less that compare :
He said that the number is: 

I am trying to find out how did he know that,
I did this 
 1- The variable i:
it can take values from 0 to n and notice that yes n and not n-1 because when it becomes n it will know that n is smaller that n-1 
so I values from 0 to N and the number of comparisons for I equals to N+1
2- The variable j: 
when i equals to zero, j can be from 1 to n and the number of comparisons is n, and it is n not n-1 because it needs to compare the last value to know that j is out of the range
when i equals to 1, j can be from 2 to n and the number of comparisons is n-1
...
...
when i equals to n-2, the j can be n-1 and n. the number of comparisons is 2
when i equals to n-1, the j can be n, but the number of comparisons is 1 (because we need to compare n < n even if the answer is false, but we need to do that comparisons to know that the answer is false)
3- The final calculation:
for i there is N + 1 calculation
for j there is n + n-1 + n-2 .... + 2 + 1 
to calculate the comparisons for j, i will take the average and multipli it with the count of numbers
the average of (n + n-1 + n-2 .... + 2 + 1 ) is (n+1)/2
the count of (n + n-1 + n-2 .... + 2 + 1 ) is (n-1)
thus, the comparisons of j is (N+1)/2) * (n-1)
the comparisons of i + j = 
(N+1) + ((N+1)/2) * (n-1)) = (N+1)(n+1)/2
but the teacher said it is (n+1) (n+2) /2
where is the wrong please?
 A: You have simply miscounted the comparisons involving $j$. It's a common mistake known as a fencepost error, a special kind of off-by-one-error.
The number of terms in $N + (N-1) + (N-2) + \dotsc + 2 + 1$ is $N$, not $N-1$, so you have altogether $\frac{(N+1)}{2}$ comparisons involving $j$, and together with the comparisons involving $i$, a total of
$$\frac{(N+1)N}{2} + (N+1) = \frac{(N+1)N}{2} + \frac{(N+1)2}{2} = \frac{(N+1)(N+2)}{2}.$$
Generally, if we have a set $\{ a_n : k \leqslant n \leqslant m\}$ of values indexed by integers, whether in a sum $\sum\limits_{n = k}^m a_n$ or for something else, the count of elements is $m - k + 1 = m - (k-1)$. It's a common mistake to forget the $+1$, confusing the length of the segment whose endpoints are $k$ and $m$ with the number of "fenceposts", which is one larger, since there is also a "fencepost" at the very beginning of the segment. There is no royal way to avoid such errors, but being aware that such errors are often made helps reducing the frequency (at least that's my experience).
