the formula here if this is used then we are getting the correct answer not with ((j-i+1)*(j-i+2))/2
Formula different?
Please, save your code on the IDE before sending the link.
This is what i see when i open the link you have shared.
I am really sorry
this is the code
Yes, it is correct.
What is your doubt? Can you explain it properly?
If you are asking, why the answer is not coming correct with the formula ((j-i+1)(j-i+2))/2 but with ((j-i+1)(j-i))/2.
then i would suggest you to dry run your code on sample input for both the formulas.
It would help you to understand this better.
Solution:
Both formulas are correct and would print the correct answer.
But, you have to keep one important thing in mind.
-
for first formula,
store array from index 1 to n
and iterate over it i.e. i=1 to n, with j=1, initially. -
As you are storing the elements in array from 0th index, thus the second formula is giving correct answer in your program.
Hope, this would help.
Give a like, if you are satisfied.
