Find only two numbers in array that evenly divide each other
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If an array is guaranteed to contain exactly one pair where one number evenly divides the other, the direct solution is to check pairs until you find the divisible one. The logic is simple: for each pair, divide the larger by the smaller and test whether the remainder is zero.
The main design question is not mathematical complexity but how much input size you expect. For small or medium arrays, a straightforward nested-loop solution is usually the clearest and perfectly fast enough.
The Basic Rule
Two numbers a and b evenly divide each other when the larger number is divisible by the smaller one with no remainder:
So if the pair is (4, 8), then 8 % 4 == 0, which means the pair qualifies.
If the pair is (5, 8), then 8 % 5 != 0, so it does not.
A Clear Brute-Force Solution
A clean solution in Python looks like this:
This returns (2, 8).
The code checks each unordered pair exactly once, which is easy to reason about and easy to verify.
If You Need the Quotient Too
Some versions of this problem want the quotient rather than the pair itself. In that case, return hi // lo:
That prints 4 because 8 / 2 = 4.
A Small Optimization with Sorting
If you want to make the search slightly more structured, sort the numbers first. Then for each number, only check larger numbers that come after it.
This does not change the worst-case time complexity much, but it can make the comparisons easier to think about because numbers[i] is always the smaller candidate.
Complexity
The simple nested-loop solution is O(n^2) because it may inspect every pair in the worst case.
That sounds expensive, but many real inputs are small enough that this is still the best answer. A more complicated optimization is not automatically better if it makes the code harder to understand and offers no meaningful speed benefit for the actual dataset.
Handling Edge Cases
A few edge cases deserve explicit thought:
- zero requires care because division by zero is invalid
- duplicate values can matter if the problem statement allows them
- negative numbers still work with modulo, but sign rules should be intentional
If zero is allowed, always guard the divisor check first:
If the input guarantees positive non-zero integers, the code becomes even simpler.
When Exactly One Pair Is Guaranteed
Many puzzle versions of this problem guarantee that exactly one divisible pair exists in each row. If you have that guarantee, returning the first valid pair is correct and efficient.
Without that guarantee, you may want to collect all valid pairs instead:
That changes the problem from "find the one pair" to "enumerate every pair."
Common Pitfalls
The biggest pitfall is checking only a % b == 0 without also considering the reverse direction. For a pair like (2, 8), only one ordering works.
Another common issue is forgetting about zero. x % 0 is an error, so any robust solution must avoid zero as the divisor.
Developers also sometimes over-optimize too early. The nested-loop solution is usually the best starting point because it is correct, readable, and sufficient for typical puzzle or interview-sized inputs.
Finally, be explicit about what the function should return: the pair, the quotient, or all qualifying pairs. Those are related but different tasks.
Summary
- Check pairs of numbers and test whether the larger is divisible by the smaller.
- A nested-loop solution is usually the clearest and most practical answer.
- Guard against division by zero if zero can appear in the input.
- If the problem guarantees exactly one pair, return the first valid match.
- Decide up front whether you want the pair itself or the quotient derived from it.

