Codechef Gcd Queries Problem Code: GCDQ Solution
Problem Link: https://www.codechef.com/problems/GCDQ
Solution in C++:
///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,a,n,q,l,r,i,j,cj;
cin>>t;
while(t--)
{
cin>>n>>q;
int ar[n+9],br[n+9],fr[n+9];
fr[0]=br[n+1]=0;
for(i=1; i<=n; i++)
{
cin>>ar[i];
fr[i]=__gcd(fr[i-1],ar[i]);
}
for(i=n; i>=1; i--)
{
br[i]=__gcd(br[i+1],ar[i]);
}
while(q--)
{
cin>>l>>r;
cout<<__gcd(fr[l-1],br[r+1])<<endl;
}
}
}
No comments