spoj ap2 doubt


I was looking for solutions online and I found this formula:
d = (thirdLastTerm-3term)/(n-5)
find a & n were easy to understand but in d, I am not getting why (n-5)

It is an AP.
First term =a.
2nd term= a+d
Third term = a+2d.

Similarly, 3rd last term = a+(n-3)d

Now, we are given sum.

sum = n/2 *(2a+(n-1)d)

3 eq, 3 variables.

Now note a few observations before we jump to the problem-

3rd term + 3rd last term = a+2d +a +(n-3)d = 2a+(n-1)d = last term + first term.

Also, sum = n/2 (2a +(n-1)d) = n/2 * (a +[a+(n-1)d]) = n/2 (first term+ last term)

==> n= Sum *2/(first term+last term)

We got n.

Now-

a+2d = k1
a+(n-3)d=k2

With n known, we can easily solve this as-

a = k1-2d
==>Subst. in eq.2, we get-

k1-2d +(n-3)d=k2
==>d=(k2-k1+2d)/n-3

With this we can find a by a=k1-2d

With all three known, the series can be very easily found.

Now as per your query-

Third term= a-2d; Third alst term = a+(n-3)d

What is their difference?

Third Last term - Third term = a+(n-3)d -(a+2d) = (n-5)d

That explains the n-5 there.

Hope this answer helped!! :slight_smile: (Feel free to get back in case of any doubts)

2 Likes

Thanks man! very well explained!!

Happy to help dear :slight_smile:

Nice explanation!

1 Like
//