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 C++ code snippet that performs modular addition?
DSA C++
int a = 17; int b = 25; int mod = 10; int result = (a + b) % mod; std::cout << result << std::endl;
Attempts:
2 left
💡 Hint
Remember to add first, then take the remainder with the modulus.
✗ Incorrect
17 + 25 = 42. 42 % 10 = 2.
❓ Predict Output
intermediateOutput of Modular Multiplication
What is the output of this C++ code that calculates modular multiplication?
DSA C++
int a = 7; int b = 8; int mod = 5; int result = (a * b) % mod; std::cout << result << std::endl;
Attempts:
2 left
💡 Hint
Multiply first, then take the remainder.
✗ Incorrect
7 * 8 = 56. 56 % 5 = 1.
❓ Predict Output
advancedModular Exponentiation Output
What is the output of this C++ code that computes modular exponentiation using a loop?
DSA C++
int base = 3; int exponent = 4; int mod = 7; int result = 1; for(int i = 0; i < exponent; i++) { result = (result * base) % mod; } std::cout << result << std::endl;
Attempts:
2 left
💡 Hint
Calculate 3^4 mod 7 step by step.
✗ Incorrect
3^4 = 81. 81 % 7 = 4.
🧠 Conceptual
advancedUnderstanding Modular Inverse
Which of the following statements about modular inverse is correct when modulus is a prime number?
Attempts:
2 left
💡 Hint
Think about the properties of prime modulus and coprimality.
✗ Incorrect
When modulus is prime, every number except multiples of modulus has an inverse because they are coprime.
🔧 Debug
expertIdentify the Error in Modular Exponentiation Code
What error will this C++ code produce when computing modular exponentiation?
DSA C++
int mod_exp(int base, int exponent, int mod) { int result = 1; while (exponent) { if (exponent & 1) { result = (result * base) % mod; } base = (base * base) % mod; exponent = exponent >> 1; } return result; } int main() { std::cout << mod_exp(2, -3, 5) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Consider what happens when exponent is negative in the while loop condition.
✗ Incorrect
The while (exponent) condition (i.e., exponent != 0) is true for negative values. Right-shifting negative signed integers (implementation-defined behavior) typically preserves the sign bit, preventing exponent from reaching zero and causing an infinite loop.
