Extended Euclidean Algorithm | Practice | GeeksforGeeks

We already know Basic Euclidean Algorithm. Now using the Extended Euclidean Algorithm, given a and b calculate the GCD and integer coefficients x, y. Using the same. x and y must satisfy the equation ax + by = gcd(a, b) .
 
Example 1:
Input:
a = 35
b = 15
Output:
5 1 -2
Explanation:
gcd(a,b) = 5
35*1 + 15*(-2) = 5

Example 2:
Input:
a = 30
b = 20
Output:
10 1 -1
Explanation:
gcd(30,20) = 10
30*(1) + 20*(-1) = 10


Your Task:
You don't need to read input or print anything. Your task is to complete the function gcd() which takes an integer N as input parameters and returns array of three numbers containing gcd, x, y in the same order.
 
Expected Time Complexity: O(log max(a,b))
Expected Space Complexity: O(1)
 
Constraints:
1 <= a,b <= 106
 
0
Debojyoti Sinha10 months ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.01
class Solution{    int extendedEuclideanAlgorithm(int a, int b, int &x, int &y)    {        if(b == 0)        {            x = 1;            y = 0;                        return a;        }                int x1, y1;        int g = extendedEuclideanAlgorithm(b, a % b, x1, y1);                x = y1;        y = x1 - (a / b) * y1;                return g;            }    public:    vector<int> gcd(int a, int b)    {        int x, y, g;                g = extendedEuclideanAlgorithm(a, b, x, y);                return {g, x, y};    }};
0
Yash Patil2 years ago
Yash Patil
static int []arr1 =new int[3];    static int x=0;    static int y=0;
    static int[] gcd(int a,int b){         int val1=GCD(a,b);         extendedGCD(a,b);         arr1[0]=val1;         arr1[1]=x;         arr1[2]=y;         return arr1;    }    static int GCD(int a,int b)    {         if (a == 0)             return b;
        return GCD(b%a, a);     }    static void extendedGCD(int a,int b)    {        if(b==0)        {            x=1;            y=0;            return;        }        extendedGCD(b,a%b);        int cx=y;        int cy=x-(a/b)*y;        x=cx;        y=cy;    }
0
Sumit Kumar2 years ago
Sumit Kumar
Java Solution:
gcd(a,b)=ax+by;gcd(b%a,a) = (b%a)x'+ay' =  {b-[b/a]a}x'+ay'        // [b/a] = (b/a) as in programming int b / int a = floor of(b/a)
comparing these two equations with coefficients of a and b:x'=y'-(b/a)x'y=x'
https://ide.geeksforgeeks.org/e4J0V0Cahk
0
shaikh nabeel2 years ago
shaikh nabeel
java solution
https://ide.geeksforgeeks.o...
0
vishal yadav3 years ago
vishal yadav
Simple sol:(0.01 sec):https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.