Sure @yatin,
As you have yourself pointed that when loop terminates j that we will get will 1 greater than correct value
So to neutralise that extra 1 in j, I have just subtracted 1 from both the brackets.
Mathematical Explanation:
The formula used in the video:
ans += ((j - i+1) * (j - i + 2))/2;
Actually j=j’-1,
Where
j: is the actual value of j required by the formula.
j’: is the incremented value that we are getting
Substituting j in the above formula:
ans += (((j’-1) - i+1) * ((j’-1) - i + 2))/2
ans += ((j’ - i) * (j’ - i + 1))/2;
Hence Proved.
Hope, it is clear now.
Give a like if you are not satisfied.