Series AP | Coding Problems
Aug 3, 2021
Problem Statement
Given the first 2 terms A1 and A2 of an Arithmetic Series.Find the Nth term of the series.
Example 1:
Input:
A1=2
A2=3
N=4
Output:
5
Explanation:
The series is 2,3,4,5,6....
Thus,4th term is 5.
Example 2:
Input:
A1=1
A2=2
N=10
Output:
10
Explanation:
The series is1,2,3,4,5,6,7,8,9,10,11..
Thus,10th term is 10.
Expected Time Complexity:O(1)
Expected Auxillary Space:O(1)
Constraints:
-104<=A1,A2<=104
1<=N<=103
Code & Algorithm:
/* author : @akash *//*
problem is:-*/#include<bits/stdc++.h>
using namespace std;#define ll long long int
#define pb push_back
#define mod 1000000007
#define ld long doublevoid solve()
{
int a;
int b;
cin>>a>>b;
int n;
cin>>n;
int d;
d=b-a;
cout<<a+(n-1)*d;
}int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
solve();
cout<<"\n";
}
return 0;
}// time complexity of this algorithm is : T(n)=O(1)