GCD of Two Numbers
An easy Math problem included in Apna College, Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Math
- Sheets
- 2
- Core for
- 13 roles
- Platform
- GeeksforGeeks
The problem
Given two positive integers, find their greatest common divisor (GCD) — the largest positive integer that divides both numbers without leaving a remainder.
Example 1
- Input
- a = 12, b = 8
- Output
- 4
- Why
- Divisors of 12 are 1,2,3,4,6,12 and divisors of 8 are 1,2,4,8. The greatest common divisor is 4.
Example 2
- Input
- a = 7, b = 5
- Output
- 1
- Why
- Since 7 and 5 are both prime and distinct, their only common divisor is 1. They are coprime.
Example 3
- Input
- a = 36, b = 60
- Output
- 12
- Why
- GCD of 36 and 60 is 12 since 12 divides both 36 (36/12=3) and 60 (60/12=5).
Constraints
- 1 <= a, b <= 10^9
How to think about it
Updated 2026-09-09Any common divisor of two integers must also divide their difference, which means the larger number can be replaced with its remainder modulo the smaller number without changing the set of shared factors. Repeating this reduction shrinks the pair at least by half every two steps until one value becomes zero, leaving the other as the answer.
Approaches, worst first
Linear downward scan
time O(min(a, b)) · space O(1)
Count down from min(a, b) to 1 and return the first number that evenly divides both. It requires zero algebra, but on large prime pairs near 10^9 it performs a billion divisions.
Euclidean remainder reductionWrite this one
time O(log(min(a, b))) · space O(1)
Repeatedly replace (a, b) with (b, a % b) until b becomes 0. The remainder operation compresses repeated subtractions into a single step, yielding the greatest common divisor in a logarithmic number of iterations.
Where people lose marks · 2
- Modulo by zero if b reaches 0 inside the loop condition rather than terminating before evaluating a % b.
- Attempting subtraction-based Euclidean reduction, which degrades to O(max(a, b)) when one number is 1 and the other is 10^9.
The theory behind it
Math — the ground this problem stands on. All Math problems
What Math is
Algorithmic math is the application of number properties, modular arithmetic, and geometric patterns to compute results without simulating every step. Rather than filling memory buffers with millions of simulated counters or walking endless loops, mathematical formulation uses identities like prime factorization, digit extraction, and greatest common divisors to jump directly to target answers through closed-form rules.
When to reach for it
Reach for mathematical techniques when problem bounds are massive, such as constraints reaching ten to the eighteenth power where simulation is impossible. Strong prompts mention prime generation, greatest common divisor calculations, modular inverses, fast exponentiation, digit reversals, or combinatorial arrangements. If brute-force counting produces memory overflow or time limit exceeded warnings, algebraic reorganization is the intended path.
How the pattern works
Focus on numeric constraints and potential overflows before writing expressions. Break numbers into constituent digits using modulo ten to peel least significant figures and integer division to shrink scale. For divisibility, apply Euclid's remainder theorem repeatedly until remainder drops to zero. Apply modulo arithmetic at every intermediate addition and multiplication stage rather than once at the end, preventing values from spilling beyond standard numeric register limits.
What each operation costs
| Operation | Time |
|---|---|
| greatest common divisor via euclidean algorithm | O(log(min(a, b))) |
| modular exponentiation by repeated squaring | O(log n) |
| prime sieve of eratosthenes up to n | O(n log log n) |
What usually goes wrong with Math
- Allowing 32-bit signed integer overflow during intermediate multiplications before modulo reduction is applied, producing negative or truncated results.
- Failing to handle negative inputs in modulo arithmetic, producing negative remainders in languages that implement truncated rather than floored division.
- Dividing by zero when the denominator or remainder becomes zero at unexpected edge boundary values like empty sets or singletons.
Which roles need this problem
Math is a core topic for these 13 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 7 more roles, including Security Engineer, Performance Engineer, Site Reliability Engineer.
Companies that have asked it
Tags taken from the problem's own GeeksforGeeks page — not a copied list.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Math problems
Problem set and role mapping as of .