Home
DSA Course
Mathematics
Modular Arithmetic
Modular Arithmetic
Visualize arithmetic operations on a circle (clock arithmetic).
Ready to calculate.
Current
0
0
1
2
3
4
5
6
7
8
9
10
11
Steps
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.Intuition
Imagine a clock. If it's 10 o'clock and you add 5 hours, it becomes 3 o'clock, not 15. This is arithmetic modulo 12. Modular arithmetic is simply "arithmetic with cycles".
Concept
- Modulo Operator (%): Returns the remainder of division.
A % M = RmeansA = k*M + R. - Addition:
(A + B) % M - Subtraction:
(A - B) % M. In code, often written as((A - B) % M + M) % Mto handle negative results. - Multiplication:
(A * B) % M. Can be visualized as repeated addition on the cycle.
How it Works
Properties:
- (A + B) % M = ((A % M) + (B % M)) % M
- (A * B) % M = ((A % M) * (B % M)) % M
- (A - B) % M = ((A % M) - (B % M) + M) % M
Step-by-Step Breakdown
Watch the visualization:
- The circle represents numbers 0 to M-1.
- Adding moves clockwise.
- Subtracting moves counter-clockwise.
- Multiplication is visualized as repeated addition (jumps) on the circle.
When to Use
- Cryptography (RSA, Diffie-Hellman).
- Hash functions (array indexing).
- Cyclic problems (days of week, rotating arrays).
- Preventing integer overflow in competitive programming (calculating modulo 10^9 + 7).
When NOT to Use
- When exact values matter, not just remainders.
- Standard continuous mathematics.
How to Identify
"Return result modulo 10^9 + 7", "Cyclic array", "Divisibility rules", "Periodic Pattern".
Stop Guessing, Start Mastering.
Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.
