Modular Arithmetic in DSA
Modular arithmetic is an important mathematical concept used in programming and Data Structures and Algorithms. It helps us work with large numbers by calculating their remainders after division. It is commonly used in competitive programming, hashing, cryptography, and problems involving large calculations.
1. What is Modular Arithmetic?
Modular arithmetic is a system of arithmetic where numbers wrap around after reaching a particular value called the modulus.
The modulo operation gives the remainder when one number is divided by another.
Notation:
a mod m = remainder when a is divided by m
Example:
17 mod 5 = 2
20 mod 6 = 2
15 mod 3 = 0
8 mod 3 = 2
In the first example, 17 divided by 5 gives a quotient of 3 and a remainder of 2.
2. Understanding the Modulo Operator in C++
In C++, the modulo or remainder operator is represented by the percentage symbol (%).
#include <iostream>
using namespace std;
int main() {
int a = 17;
int b = 5;
cout << a % b;
return 0;
}
Output:
2
The expression a % b returns the remainder when a is divided by b.
3. Important Properties of Modular Arithmetic
Modular arithmetic follows several useful properties. These properties allow us to simplify calculations.
3.1 Addition
The modulo of a sum can be calculated by first taking the modulo of each number.
(a + b) % m = ((a % m) + (b % m)) % m
Example:
(17 + 13) % 5
= 30 % 5
= 0
Using the property:
((17 % 5) + (13 % 5)) % 5
= (2 + 3) % 5
= 0
3.2 Subtraction
Subtraction can also be performed using modular values.
(a - b) mod m
In programming, the result may become negative. To obtain a non-negative result, add the modulus before applying modulo again.
((a % m) - (b % m) + m) % m
Example:
(3 - 7) mod 5
= -4 mod 5
= 1
3.3 Multiplication
Multiplication follows a similar property.
(a * b) % m = ((a % m) * (b % m)) % m
Example:
(12 * 8) % 5
= 96 % 5
= 1
Using modular values:
((12 % 5) * (8 % 5)) % 5
= (2 * 3) % 5
= 1
3.4 Exponentiation
Modular arithmetic can also be applied to powers.
(a^b) % m
For example:
2^5 % 7
= 32 % 7
= 4
For very large powers, calculating the complete power first can be inefficient or cause integer overflow. Fast exponentiation solves this problem and is covered in the next topic.
4. Negative Numbers and Modulo in C++
One important difference between mathematical modulo and the C++ remainder operator is how negative numbers are handled.
In C++, the remainder can be negative when the dividend is negative.
#include <iostream>
using namespace std;
int main() {
cout << -7 % 5;
return 0;
}
Output:
-2
In many DSA problems, we require a non-negative modulo result. We can normalize the result using the following formula:
((a % m) + m) % m
C++ example:
#include <iostream>
using namespace std;
int main() {
int a = -7;
int m = 5;
int result = ((a % m) + m) % m;
cout << result;
return 0;
}
Output:
3
5. Modular Arithmetic to Handle Large Numbers
In competitive programming, some problems involve numbers that grow extremely large. Storing such numbers directly in standard integer data types may cause overflow.
Modular arithmetic allows us to keep intermediate values within a manageable range by applying modulo after calculations.
Example:
Suppose we need to calculate the product of two numbers modulo 1000000007.
#include <iostream>
using namespace std;
int main() {
long long a = 100000;
long long b = 200000;
long long mod = 1000000007;
long long result = ((a % mod) * (b % mod)) % mod;
cout << result;
return 0;
}
Output:
999999972
Note: Using long long does not prevent every possible overflow. For very large operands, even their product may exceed the range of the data type. Additional techniques may be required.
6. Why is 1000000007 Commonly Used?
You will frequently see the number 1000000007 (109 + 7) in competitive programming problems.
There are two important reasons:
- It is a prime number, which makes it useful for many modular arithmetic operations.
- It is large enough to reduce unwanted collisions and keep many intermediate values manageable.
Other commonly used moduli include 998244353 and 1000000009.
7. Where is Modular Arithmetic Used?
- Competitive Programming: Handling large answers in mathematical and algorithmic problems.
- Hashing: Calculating hash values for strings and data structures.
- Cryptography: Performing mathematical operations used in encryption systems.
- Combinatorics: Calculating combinations and permutations under a given modulus.
- Number Theory: Solving problems involving divisibility, remainders, and modular equations.
8. Common Mistakes to Avoid
- Confusing the modulo operation with ordinary division.
- Forgetting to apply modulo during repeated calculations.
- Ignoring negative remainders in C++.
- Assuming that division follows the same modular property as addition and multiplication.
- Applying modulo after multiplication without considering possible integer overflow.
9. Time and Space Complexity
The modulo operator on standard integer types is generally treated as a constant-time operation.
| Operation | Time Complexity |
|---|---|
| Addition with modulo | O(1) |
| Subtraction with modulo | O(1) |
| Multiplication with modulo | O(1) |
| Remainder operation | O(1) |
10. Quick Revision
- Modular arithmetic works with remainders after division.
- The modulo operator in C++ is represented by %.
- Addition, subtraction, and multiplication can be performed using modular values.
- C++ can return negative remainders for negative operands.
- Modulo is widely used to handle large numbers in DSA problems.
- Fast exponentiation helps calculate powers efficiently under a modulus.
11. Practice Questions
Beginner Level
- What is the result of 25 % 4?
- Calculate (15 + 12) % 7.
- Calculate (9 × 8) % 5.
- What is the output of -10 % 3 in C++?
- Write a C++ program to find the remainder of two numbers.
Intermediate Level
- Write a program to calculate the sum of two numbers under a given modulus.
- Write a program to calculate the product of two numbers using modular arithmetic.
- Convert a negative remainder into a non-negative modulo result.
- Explain why modular arithmetic is useful when working with large numbers.
- Why can ordinary division not always be performed directly under a modulus?
Interview Questions
- What is modular arithmetic, and why is it used in DSA?
- Explain the properties of modular addition and multiplication.
- What is the difference between modulo and remainder in programming languages?
- Why is 1000000007 frequently used as a modulus?
- How can modular arithmetic help prevent integer overflow?