educative.io

Https://discuss.educative.io/tag/solution-review-1-euclidean-algorithm__divide-and-conquer__algorithms-for-coding-interviews-in-java

public static int GCD(int a, int b, int x, int y)
{
if (a == 0)
{
x = 0;
y = 1;
return b;
}
int x1 = 1, y1 = 1; // storing results of recursive call
int gcd = GCD(b%a, a, x1, y1);
x = y1 - (b/a) * x1; // Update values using results of recursive
y =x1;*
return gcd;
}

What does computing x and y in x = y1 - 9b/a) * x1;
y = x1;

Hi @Vincent_Mwagiru

Your query is not quite understandable. Can you please elaborate?

I am not understanding why we need to reassign x and y after the gcd call… just before we return gcd . Whats the purpose of the two lines below

x = y1 - (b/a) * x1;
y =x1;

Hi @Vincent_Mwagiru,
The purpose of the two lines was supposed to calculate the values of coefficients x and y through recursive updates. However, the above code doesn’t recursively return or display any of these coefficients.
You will be pleased to know that the code has been updated on the platform, and you can get an idea of how x and y get recursively calculated.