In this program,we need to find hcf and lcm of a number and must use vector for it and time complexity should be O(log n) and using c++.
class Solution
{
public:
vector<long long> lcmAndGcd(long long A , long long B)
{
long long min,hcf,lcm;
// code here
if(A<B)
min=A;
else
min=B;
for(long long i=min;i>=1;i--)
{
if((A%i==0)&&(B%i==0))
{
hcf=i;
break;
}
}
lcm=(A*B)/hcf;
vector<long long> v;
v.push_back(lcm);
v.push_back(hcf);
return v;
}
};
I did this but I am not able to do it with time complexity O(logN)
The C++17 standard library provides
std::gcdandstd::lcm. I strongly advise against implementing them yourself. This leads to slow, error-prone and unnecessarily complicated code.