Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Greatest Common Divisor Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Greatest Common Divisor | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given 2 numbers, find their Greatest Common Divisor (GCD).

Naive Approach

We can loop backward from the smaller of the two numbers, and check if the current number divides both the numbers. As soon as we find such a number, we return it.

Analysis

  • Time Complexity: O(min(first_number, second_number))
  • Space Complexity: O(1)

Implementation

C++
int gcd(int firstNum, int secondNum) {
   if(firstNum > secondNum) {
       int temp = firstNum;
       firstNum = secondNum;
       secondNum = temp;
   }
   for(int i = firstNum; i >= 1; i--) {
       if(firstNum % i == 0 && secondNum % i == 0) {
           return i;
       }
   }
   return 1;
}
Java
class Solution {
   int gcd(int firstNum, int secondNum) {
       if(firstNum > secondNum) {
           int temp = firstNum;
           firstNum = secondNum;
           secondNum = temp;
       }
       for(int i = firstNum; i >= 1; i--) {
           if(firstNum % i == 0 && secondNum % i == 0) {
               return i;
           }
       }
       return 1;
   }
}

Optimal Approach

To this problem optimally, we will use the Euclidean Algorithm. The algorithm is as follows,

  • Base Case: gcd(a, b) = a (if b = 0)
  • General Case: gcd(a, b) = gcd(a, a mod b)

The algorithm converges in logarithmic time and returns the gcd of the two numbers.

Analysis

  • Time Complexity: O(log(min(first_number, second_number))
  • Space Complexity: O(1)

Implementation

C++
int euclideanGCD(int firstNum, int secondNum) {
   if(firstNum == 0) {
       return secondNum;
   }
   return euclideanGCD(secondNum % firstNum, firstNum);
}
int gcd(int firstNum, int secondNum) {
   return euclideanGCD(firstNum, secondNum);
}
Java
class Solution {
   int euclideanGCD(int firstNum, int secondNum) {
       if(firstNum == 0) {
           return secondNum;
       }
       return euclideanGCD(secondNum % firstNum, firstNum);
   }
   int gcd(int firstNum, int secondNum) {
       return euclideanGCD(firstNum, secondNum);
   }
}
Related Content
Excel Column Number
Exponentiation With Modulus
Exponentiation With Modulus
Matrix Paths
Primes upto N
Trailing Zeroes
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet