Find pairs in an array such that ab k , where k is a given integer
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The most natural interpretation of this problem is: find pairs (a, b) in an array such that a % b == k for a given integer k. The condition looks simple, but it hides useful arithmetic structure that can help you move beyond the obvious O(n^2) double loop.
Understanding The Modulo Condition
If a % b == k, then there exists an integer q such that:
For positive integers, this immediately tells you two things:
- '
bcannot be0, because modulo by zero is invalid' - if
b <= k, thena % bcannot equalk
So for each candidate b, the only possible matching values of a are k + b, k + 2b, k + 3b, and so on.
The Brute-Force Solution
If the array is small, the straightforward solution is perfectly fine:
This is easy to read, easy to test, and hard to get wrong. Its cost is O(n^2), which becomes expensive on large arrays.
Using Arithmetic To Skip Impossible Pairs
For positive integers, you can do better by using the structure of the equation. Build a frequency table of the array values. Then, for each candidate b, generate only the a values that could possibly satisfy a % b == k.
This does not test every pair. Instead, it jumps over impossible values and only checks arithmetic progressions that can produce the right remainder.
Counting Pairs Instead Of Storing Them
Many interview problems only ask for the count. In that case, returning every pair wastes memory. Counting is usually simpler:
This is a better choice when the number of valid pairs could be much larger than the array itself.
Defining What Counts As A Pair
Before optimizing, decide exactly what the problem means:
- Are pairs ordered, so
(a, b)is different from(b, a)? - Can the same array element be reused, or must indices be distinct?
- Do duplicates count multiple times?
- Are negative numbers allowed?
These questions matter because modulo is not symmetric and language rules for negative operands differ. In Python, the remainder has the sign of the divisor; in other languages, behavior may differ. If the original array can contain negatives, use a definition that matches the target language rather than assuming mathematics alone will settle it.
When The Brute-Force Version Is The Right Answer
The arithmetic optimization is useful when values are positive and the value range is manageable. If the array is tiny, or if clarity matters more than raw speed, the double loop may still be the better solution. A readable O(n^2) implementation often beats a clever one that nobody trusts.
Optimization should follow the constraints. If n is only a few hundred, the simpler algorithm is often the right engineering choice.
Common Pitfalls
- Forgetting that
bcannot be zero. - Ignoring the fact that
b > kis required in the positive-integer case. - Treating
(a, b)and(b, a)as interchangeable even though modulo is directional. - Counting values instead of indices without checking whether duplicates should produce multiple pairs.
- Applying the positive-integer optimization when the array may contain negatives and the language defines
%differently.
Summary
- '
a % b == kimpliesa = q * b + kfor some integerq.' - The brute-force double loop is the clearest solution for small inputs.
- For positive integers, you can generate only plausible
avalues for eachb. - Counting matches is often cheaper than returning every pair.
- Always define how zero, negatives, duplicates, and ordering should be handled.

