LCM And GCD | Practice | GeeksforGeeks

Given two numbers A and B. The task is to find out their LCM and GCD.
 
Example 1:
Input:
A = 5 , B = 10
Output:
10 5
Explanation:
LCM of 5 and 10 is 10, while
thier GCD is 5.
Example 1:
Input:
A = 14 , B = 8
Output:
56 2
Explanation:
LCM of 14 and 8 is 56, while
thier GCD is 2.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function lcmAndGcd() which takes an Integer N as input and returns a List of two Integers, the required LCM and GCD.
 
Expected Time Complexity: O(log(min(A, B))
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= A,B <= 1018
0
mayank180919991 month ago
   vector<long long> lcmAndGcd(long long A , long long B) {
        // code here
        vector<long long>v;
        int a=__gcd(A,B);
        v.push_back((A*B)/a);
        v.push_back(a);
      return v;  
    }
0
himanshulanjewar241 month ago
class Solution:
     def gcd(self, a, b):
          mini min(a,b) 
          rem = max(a, b)%mini
          if rem == 0:
               return mini
          else:
               return self.gcd(rem,mini)
 
     def 1cmAndGcd(self, A, B):
          #code here
          l = [ ]
          l.append((A*B)//self.gcd(A,B))
          l.append(self.gcd(A,B))
          return l
0
soham0071 month ago
 long long gcd(int a,int b){
      return b==0?a:gcd(b,a%b);
  }
    vector<long long> lcmAndGcd(long long A , long long B) {
        // code here
        vector<long long>arr;
        long long st = gcd(A,B);
        
        
        long long st1 = (A*B)/st;
        
        arr.push_back(st1);
        arr.push_back(st);
        return arr;
    }
0
sharmarahul996111 month ago
       long long C=A;       long long D=B;       while(A!=B){           if(A>B){               A=A-B;           }if(B>A){               B=B-A;           }       }       long long gcd = A;       long long lcm = (C*D)/gcd;       return {lcm , gcd};
+1
itsmeshahid2 months ago
vector<long long> lcmAndGcd(long long A , long long B) {              long long gcd=1;              long long maxi=max(A,B);       long long mini=min(A,B);              while(maxi%mini!=0){           long long rem=maxi%mini;           maxi=mini;           mini=rem;       }          gcd=mini;              long long lcm=A*B/gcd;              return {lcm, gcd};   }
+1
anuraggulati2412 months ago
c++ optamized solution do not make recursive calls in this gcd function because space complexity is increase and in iterative sapace is constant and time is linear .. so no need for recursive call ....
 
 
long long gcd(long long int a , long long int b) {
      
      if(a==b) {
          return b;
      }
      
      if(a%b == 0) {
          return b;
      }
       if(b%a == 0) {
           return a;
       }
       while(a != b) {
         if(a>b) {
           a = a-b;
        }
        else {
            b = b - a;
        }
      }
      return b; 
  }
    vector<long long> lcmAndGcd(long long A , long long B) {
        // code here
        vector<long long> vec1 = {(A*B)/gcd(A,B) , gcd(A,B)};
        return vec1;
    }
 
0
aakasshuit2 months ago
 static long GCD(Long A, Long B){
        if(A==0){
            return B;
        }
        return GCD(B%A,A);
    }
    static long LCM(Long A,Long B){
        return (A*B)/GCD(A,B);
    }
    static Long[] lcmAndGcd(Long A , Long B) {
        // code here
        Long[] ans = new Long[2];
        ans[0]= LCM(A,B);
        ans[1]= GCD(A,B);
        return ans;
+1
asifsaba5812 months ago
 long long GCD(long long A,long long B){
      
      
      if(B==0){
          
          return A;
      }
      return GCD(B,A%B);
  }
  
  long long LCM(long long A,long long B){
      
      long long res=A*B;
    
      return (res/GCD(A,B));
  }
  
    vector<long long> lcmAndGcd(long long A , long long B) {
        
        vector<long long>ans;
        
        ans.push_back(LCM(A,B));
        
         ans.push_back(GCD(A,B));
        
        return ans;
        
    }
+2
imohdalam2 months ago
Java
class Solution {
    static Long[] lcmAndGcd(Long A , Long B) {
        Long[] ans = new Long[2];
        
        ans[1] = gcd(A, B);
        ans[0] = lcm(A, B, ans[1]);
        
        return ans;
    }
    
    static long lcm(long A, long B, long gcd){
        return A*B/gcd;
    }
    
    static long gcd(long A, long 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
u6twqtxu7rv0cxwmqxqnrtghetxkoua4ogrydvxe2 months ago
For c++
class Solution {
     public: long long gcd(long long a,long long b){
         if(a==0){
             return b;
         } else if(b==0){
             return a;
             
         }
        if (a>b){
           return gcd(a%b,b);
       }else{
           return gcd(a,b%a);
       }
   }
    vector<long long> lcmAndGcd(long long A , long long B) {
        
       vector<long long> vector2 = {(A*B)/gcd(A,B),gcd(A,B)};
       return vector2;
       // code here
    }
};
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.