Challenge - 5 Problems
Modular Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Modular Addition
What is the output of the following Java code snippet that performs modular addition?
DSA Java
int a = 17; int b = 23; int mod = 10; int result = (a + b) % mod; System.out.println(result);
Attempts:
2 left
💡 Hint
Remember to add first, then take the remainder when divided by mod.
✗ Incorrect
Adding 17 and 23 gives 40. Taking 40 modulo 10 gives the remainder 0, so the correct output is 0, option A.
❓ Predict Output
intermediateModular Multiplication Result
What will be printed by this Java code that calculates modular multiplication?
DSA Java
int x = 7; int y = 8; int mod = 12; int result = (x * y) % mod; System.out.println(result);
Attempts:
2 left
💡 Hint
Multiply first, then take the remainder with mod.
✗ Incorrect
7 * 8 = 56. 56 % 12 = 8, so output should be 8. So option C is correct.
🧠 Conceptual
advancedProperties of Modular Arithmetic
Which of the following statements about modular arithmetic is TRUE?
Attempts:
2 left
💡 Hint
Think about how multiplication and addition behave under modulus.
✗ Incorrect
Modular multiplication distributes over modular addition: (a * (b + c)) % m = ((a * b) % m + (a * c) % m) % m. Modular addition is associative, so A is false. Modular subtraction can produce negative results before taking modulus, so C is false. Modular division is not always defined because inverse may not exist, so D is false.
🔧 Debug
advancedIdentify the Error in Modular Exponentiation Code
What error does this Java code produce when trying to compute (base^exp) % mod?
DSA Java
int base = 3; int exp = 4; int mod = 5; int result = 1; for (int i = 0; i < exp; i++) { result = (result * base) / mod; } System.out.println(result);
Attempts:
2 left
💡 Hint
Check the operator used for modular reduction inside the loop.
✗ Incorrect
The code uses division '/' instead of modulus '%' to reduce the result. This causes incorrect calculation but no runtime error. So option D is correct.
🚀 Application
expertNumber of Distinct Values in Modular Sequence
Given the Java code below, how many distinct values will be printed?
DSA Java
int mod = 7; Set<Integer> values = new HashSet<>(); for (int i = 0; i < 20; i++) { int val = (3 * i) % mod; values.add(val); } System.out.println(values.size());
Attempts:
2 left
💡 Hint
Consider the properties of multiplication modulo a prime number.
✗ Incorrect
Since 3 and 7 are coprime, multiplying by 3 modulo 7 cycles through all residues 0 to 6. So all 7 distinct values appear.
