HCF problem wrong answer in a testcase

https://ide.codingblocks.com/#/s/13952
https://hack.codingblocks.com/contests/c/452/57
#include
using namespace std;
int gcd(long long int a,long long int b){
if(b==0)
return a;
return gcd(b,a%b);
}
int main() {
long long int a,b,c,n,i,res;
cin>>n>>a;
res=101;
for(i=1;i<n;i++){
cin>>b;
c=gcd(a,b);
a=b;
if(c<res)
res=c;
}
cout<<res;
}

Hi Amritanshu,

There is some issue in your logic. You cannot take gcd of two different nos. at each iteration and expect to get the gcd of all numbers…Think about this piece of code a=b; You are supposed to look into gcd of one set of numbers and figure out a way to use it to find the the subsequent gcd’s.

1 Like