Minimum number X such that X P N
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This kind of question is usually shorthand for finding the smallest X such that X % P = N. Once the modulo meaning is made explicit, the problem becomes simple number theory: every solution belongs to one residue class, and the smallest non-negative representative is immediate.
The Direct Mathematical Answer
If the condition is X % P = N, then every solution has the form:
X = kP + N
for some integer k, as long as N is a valid remainder for modulus P.
That gives the first rule:
- if
0 <= N < P, the minimum non-negativeXisN - if
Nis outside that range, there is no integerXwhose remainder is exactlyNunder the usual modulo definition
Examples:
- for
P = 5andN = 2, the smallestXis2 - for
P = 12andN = 5, the smallestXis5 - for
P = 7andN = 9, there is no valid solution because9is not a legal remainder modulo7
Why the Answer Is So Short
Modulo arithmetic groups integers into repeating classes. All numbers congruent to N modulo P differ by multiples of P.
So for P = 4 and N = 1, the full solution set is:
- '
1' - '
5' - '
9' - '
13' - and so on
The smallest non-negative member of that class is 1, so that is the answer.
A small Python helper makes the rule explicit:
This prints 2 and 5.
Watch for Problem Statement Ambiguity
Some exercises quietly assume X must be positive rather than non-negative. That changes one edge case.
If N = 0:
- minimum non-negative
Xis0 - minimum positive
XisP
You can encode that choice directly:
This prints 7 and 3.
That distinction matters in programming contests and interview questions because authors do not always say whether zero counts.
Connection to Programming
In code, this pattern shows up whenever work wraps around a cycle, such as:
- indexing circular buffers
- scheduling repeating tasks
- hashing into buckets
- stepping through periodic states
If you know the desired remainder class, the smallest representative is the first valid state in that cycle.
For example, finding the first non-negative number whose remainder mod 8 is 6 is exactly the same as asking for the first position in an eight-step cycle that lands on state 6.
Common Pitfalls
The most common mistake is forgetting that a remainder must be in the standard range 0 through P - 1 when P is positive. Asking for X % 7 = 9 is invalid under that definition.
Another mistake is ignoring whether X may be zero. If the problem asks for the smallest positive solution, N = 0 no longer maps to 0.
Developers also sometimes mix mathematical modulo with language-specific behavior for negative operands. Many languages define % on negative numbers in implementation-specific or language-specific ways, so be careful if the problem allows negative values.
Finally, do not overcomplicate this particular task. Once the expression is interpreted as a modulo equation, the minimum non-negative solution is usually just the remainder itself.
Summary
- Interpreting the question as
X % P = Ngives a simple residue-class problem. - If
0 <= N < P, the smallest non-negative solution isX = N. - If
Nis not a valid remainder, there is no solution under the usual modulo definition. - If the problem requires positive
X, theN = 0case becomesX = P. - Most confusion comes from ambiguous wording, not from difficult math.

