GCD of two numbers | Practice | GeeksforGeeks

Given two positive integers A and B, find GCD of A and B.

Example 1:
Input: A = 3, B = 6
Output: 3
Explanation: GCD of 3 and 6 is 3

Example 2:
Input: A = 1, B = 1
Output: 1
Explanation: GCD of 1 and 1 is 1

Your Task:  
You don't need to read input or print anything. Complete the function gcd() which takes two positive integers as input parameters and returns an integer.

Expected Time Complexity: O(log(min(A, B)))
Expected Auxiliary Space: O(1) 

Constraints:
1 ≤ A, B ≤ 109
+1
rapuritejain 10 hours
class Solution:   def gcd(self, A, B):       if B== 0:           return A       else:           return (ob.gcd(B,A%B))
0
rsbly7300951 week ago
Javascript solution
class Solution{   gcd(A , B)   {       if (B === 0)           return A;       let a = this.gcd(B, A % B);       return a;
   }   }
0
mankesh0161 week ago
return (A%B)? gcd(B,A%B) : B;
0
mainakdeykol1 week ago
class Solution
{
   public int gcd(int A , int B) 
   { 
       //code here
       if(A==B) return A;
       if(A>B){
           return gcd(A-B,B);
       }
       return gcd(A,B-A);
   } 
}
0
abhigyanpatek2 weeks ago
Simple C++ O(log min(A,B)):
int gcd(int A, int B) {    while(A%B != 0){           int temp = A%B;           A = B;           B = temp;        }    return B;}
0
abhinav904442 weeks ago
// { Driver Code Starts#include <bits/stdc++.h>using namespace std;
// } Driver Code Ends//User function Template for C++class Solution{public:   int gcd(int A, int B) {     // code here      if (B==0)       return A;   return gcd(B,A%B);    } };
// { Driver Code Starts.
int main() {   int t;   cin >> t;   while (t--)   {       int A, B;       cin >> A >> B;       Solution ob;       cout <<  ob.gcd(A, B) << "\n";   }   return 0;}  // } Driver Code Ends
+3
kartikeyashokgautam3 weeks ago
JAVA Solution :-
    public int gcd(int A , int B)    {       if(A==0)       return B;              if(B==0)       return A;              if(A==B)       return A;                 if (A > B)           return gcd(A-B, B);                  return gcd(A, B-A);   } 
0
mail2rajab011 month ago
// Simple Java Solution
Total Time Taken: 0.2/1.3
 
class Solution{   public int gcd(int A , int B)    {        //code here       if(B==0)           return A;                  return gcd(B,A%B);   } }
0
0niharika22 months ago
if(B==0)        return A;    return gcd(B,A%B);
0
ankitparashxr2 months ago
java
if(b==0)       {           return a;       }       return gcd(b,a%b);
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.