Modular Arithmetic

Clock arithmetic and mod properties. You will see why mod 10^9 + 7 appears in contests and how to compute huge powers without overflow.

46 lessons
143 min
Codeforces: 1000-1500LeetCode: 1400-1700

Lessons

1. Intro

(The Goal)

2m

2. Clock Arithmetic

(Time wraps around 12)

5m

3. The Mod Operator

(Remainder after division)

3m

4. Negative Numbers

(Language differences matter)

3m

5. Congruence Notation

(Same remainder means equal)

3m

6. Modular Addition

(Add then mod, or mod then add)

5m

7. Modular Multiplication

(Multiply then mod, or mod then multiply)

3m

8. Modular Subtraction

(Watch for negative results)

3m

9. Why Mod in Contests

(Overflow prevention)

4m

10. Problem - Power of Two

(Check if number is power)

2m1 problems

11. Power of Two - Insight

(Repeated division by 2)

5m

12. Power of Two - Algorithm

(Loop until n becomes 1)

2m

13. Power of Two - Implementation

(15 lines)

3m1 problems

14. Quiz: Modular Basics

(Knowledge check)

2m1 problems

15. Repeated Operations

(Take mod at each step)

3m

16. Problem - Add Digits

(Digital root via mod 9)

2m1 problems

17. Brute Force Approach

(Loop until single digit)

3m

18. Add Digits - Insight

(Digital root and mod 9)

4m

19. Add Digits - Algorithm

(O(1) formula)

2m

20. Add Digits - Implementation

(3 lines)

3m1 problems

21. Quiz: Modular Addition

(Knowledge check)

2m1 problems

22. Fast Exponentiation

(Computing large powers)

3m

23. Example - Fast Exponentiation

(Computing 2^10)

3m

24. Modular Exponentiation Algorithm

(Binary exponentiation)

4m

25. Problem - Pow(x, n)

(LeetCode 50)

2m1 problems

26. Pow(x, n) - Insight

(Handle negative exponent)

4m

27. Pow(x, n) - Algorithm

(Fast exponentiation with negatives)

2m

28. Pow(x, n) - Implementation

(12 lines)

3m1 problems

29. Modular Inverse

(Division in modular arithmetic)

4m

30. Example - Modular Inverse

(Inverse of 3 mod 7)

3m

31. Quiz: Modular Multiplication

(Knowledge check)

2m1 problems

32. Common Moduli

(Values you see often)

4m

33. Overflow Prevention

(Why mod is not optional)

4m

34. Practice - Happy Number

(LeetCode 202)

3m1 problems

35. Practice - Power of Three

(LeetCode 326)

3m1 problems

36. Practice - Excel Sheet Column Number

(LeetCode 171)

3m1 problems

37. Practice - Nth Digit

(LeetCode 400)

3m1 problems

38. Quiz: Fast Exponentiation

(Knowledge check)

2m1 problems

39. Real World Uses

(Beyond contests)

4m

40. Common Mistakes

(What to avoid)

3m

41. Debugging Tips

(When your mod code fails)

4m

42. Problem - Super Pow

Modular exponentiation challenge

2m1 problems

43. Super Pow - Insight

Break down large exponents

4m

44. Super Pow - Algorithm

Digit by digit computation

3m

45. Super Pow - Implementation

The pseudocode

5m1 problems

46. Section Recap

What we learned

2m

Ready to start learning?

Access all 46 lessons with interactive content and progress tracking.