A different approach to the problem


#1
vector<int> sorted(vector<int> A){
int temp;
for(int i=0;i<A.size()-1;i++)
{
    for(int j=i+1;j<A.size();j++)
    {
        if(A[i]>A[j])
        {
            temp=A[i];
            A[i]=A[j];
            A[j]=temp;
        }
    }
}
return A;

}
int Solution::gcd(int n1, int n2) {
if(n1==0)
return n2;
if(n2==0)
return n1;
int result =1;
vector B;B.push_back(n2);
vector A;A.push_back(n1);
for(int i=2;ii<=n1;i++)
{
if(n1%i==0)
{
if(i
i!=n1)
{
A.push_back(i);
A.push_back(n1/i);
}
else
A.push_back(i);
}
}
for(int i=2;ii<=n2;i++)
{
if(n2%i==0)
{
if(i
i!=n2)
{
B.push_back(i);
B.push_back(n2/i);
}
else
B.push_back(i);
}
}

if(A.size()==0||B.size()==0)
    return 1;

A=sorted(A);B=sorted(B);



for(int i=0;i<A.size();i++)
{
    for(int j=0;j<B.size();j++)
    {
        if(A[i]==B[j])
            result=A[i];
    }
}

return result;

}