Print all Divisors
An easy Math problem included in Apna College, Striver A2Z. 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 a positive integer n, return a sorted array of all its divisors — numbers that divide n evenly without leaving a remainder.
Example 1
- Input
- n = 12
- Output
- [1, 2, 3, 4, 6, 12]
- Why
- The divisors of 12 are all numbers that divide 12 evenly: 1, 2, 3, 4, 6, and 12.
Example 2
- Input
- n = 10
- Output
- [1, 2, 5, 10]
- Why
- The divisors of 10 are 1, 2, 5, and 10.
Example 3
- Input
- n = 1
- Output
- [1]
- Why
- The only divisor of 1 is 1 itself.
Constraints
- 1 <= n <= 10^6
How to think about it
Updated 2026-09-09Divisors naturally come in complementary pairs: whenever d divides n evenly, n / d is also a divisor. One partner in every pair must be less than or equal to sqrt(n), so scanning up to the square root reveals the complete set of divisors.
Approaches, worst first
Full linear scan
time O(n) · space O(1)
Test every integer d from 1 to n. Any value with n % d == 0 is added to the result. It produces already-sorted elements but performs n division checks, which is sluggish for n = 10^6.
Square root pairing with sort
time O(sqrt(n) log(d(n))) · space O(1)
Check integers d from 1 up to sqrt(n). When d divides n, collect both d and n / d (taking care not to duplicate when d × d == n). Sort the accumulated array before returning.
Two-pass ordered collectionWrite this one
time O(sqrt(n)) · space O(1)
Scan 1 to sqrt(n) pushing small divisors into the output, then scan backwards from floor(sqrt(n)) to 1 pushing complementary partners n / d without duplicates. The result is generated in strictly sorted order without a sorting step.
Where people lose marks · 3
- Adding duplicate entries when n is a perfect square and d == n / d.
- Returning divisors in unsorted order when using the paired square-root scan, violating the sorted output requirement.
- Floating-point inaccuracies when calculating `sqrt(n)` can lead to skipping the square root boundary; use `d * d <= n` instead.
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.
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 .