![]() |
VOOZH | about |
Given two integers a1 and a2, the first and second terms of an Arithmetic Series respectively, the problem is to find the nth term of the series.
Examples :
Input : a1 = 2, a2 = 3, n = 4
Output : 5
Explanation : The series is 2, 3, 4, 5, 6, .... , thus the 4th term is 5.Input : a1 = 1, a2 = 3, n = 10
Output : 19
Explanation: The series is: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21..... Thus,10th term is 19.
Table of Content
In an Arithmetic Series, the difference between all pair of consecutive terms is same, for example, 2, 5, 8, 11, 14,,,,, The common difference is 3.
5
Time Complexity- O(n)
Auxiliary Space - O(1)
To find the nth term in the Arithmetic Progression series we use the simple formula .
We know the Arithmetic Progression series is like = 2, 3, 4, 5, 6. …. …
In this series 2 is the first term and 3 is the second term of the series .
Common difference = a2 - a1 = 3 – 2 = 1 (Difference common in the series).
so we can write the series as :
t1 = a1
t2 = a1 + (2-1) * d
t3 = a1 + (3-1) * d
.
.
.
tN = a1 + (n-1) * dtN = a1 + (n-1) * (a2-a1)
5
Time Complexity- O(1)
Auxiliary Space - O(1)