Time and Space Complexity

Find the Complexity of the given code.

int gcd(int n, int m)
{
if (n%m ==0) return m;
if (n < m) swap(n, m);
while (m > 0)
{
n = n%m;
swap(n, m);
}
return n;
}

Why is it’s complexity is coming out to be O(log n) isn’t it should be O(n)??

hi @mvermav19, u can read here https://www.geeksforgeeks.org/time-complexity-of-euclidean-algorithm/#:~:text=The%20time%20complexity%20of%20this,and%20b%20are%20two%20integers. for better understanding

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.