SerSeries GP | Coding Problem

Akash Kumar
1 min readAug 3, 2021

Problem Statement

Given the A and R i,e first term and common ratio of a GP series. Find the Nth term of the series.

Example 1:

Input: A = 2, R = 2, N = 4
Output: 16
Explanation: The GP series is
2, 4, 8, 16, 32,... in which 16
is th 4th term.
problem solving

Example 2:

Input: A = 4, R = 3, N = 3
Output: 36
Explanation: The GP series is
4, 12, 36, 72,.. in which 36 is
the 3rd term.

Expected Time Complexity: O( LogN)
Expected Space Complexity: O(1)

Constraints:
1 <= A, R, N <= 1000000

Code & Algorithm:

/* author : @akash *//* 
c++ code
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mod 1000000007
#define ld long double
void solve()
{
int a;
int R;
cin>>a>>R;
int n;
cin>>n;
cout<<a*pow(R,n-1);
}
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(LogN)

Thank You.

Akash Kumar

Software Engineer

--

--

Akash Kumar

Student of Computer Science & Engineering at Moradabad Institute of Technology.