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 TypeScript code that performs modular addition?
DSA Typescript
const a = 17; const b = 23; const mod = 10; const result = (a + b) % mod; console.log(result);
Attempts:
2 left
💡 Hint
Remember to add first, then take the remainder when divided by the modulus.
✗ Incorrect
17 + 23 = 40. 40 % 10 = 0. The output is 0.
❓ Predict Output
intermediateOutput of Modular Multiplication
What is the output of this TypeScript code that calculates modular multiplication?
DSA Typescript
const x = 7; const y = 8; const mod = 12; const result = (x * y) % mod; console.log(result);
Attempts:
2 left
💡 Hint
Multiply first, then take the remainder after dividing by the modulus.
✗ Incorrect
7 * 8 = 56. 56 % 12 = 8 (56 - 4*12 = 56 - 48 = 8). The output is 8.
🧠 Conceptual
advancedUnderstanding Modular Inverse
Which of the following statements correctly describes the modular inverse of a number 'a' modulo 'm'?
Attempts:
2 left
💡 Hint
Think about what multiplying by the modular inverse achieves.
✗ Incorrect
The modular inverse of 'a' modulo 'm' is a number 'x' such that (a * x) % m = 1. This exists only if 'a' and 'm' share no common factors other than 1 (coprime).
🔧 Debug
advancedIdentify the Error in Modular Exponentiation Code
What error will this TypeScript code produce when computing modular exponentiation?
DSA Typescript
function modExp(base: number, exponent: number, mod: number): number {
let result = 1;
for (let i = 0; i < exponent; i++) {
result = (result * base) % mod;
}
return result;
}
console.log(modExp(2, -3, 5));Attempts:
2 left
💡 Hint
Check how the loop behaves with a negative exponent.
✗ Incorrect
The loop runs from 0 to exponent-1. If exponent is negative, the loop does not run at all, so result remains 1.
🚀 Application
expertNumber of Distinct Remainders
Given an array of integers and a modulus 'm', how many distinct remainders can be produced by taking each element modulo 'm'?
DSA Typescript
const arr = [12, 25, 7, 14, 30]; const m = 10; const remainders = new Set<number>(); for (const num of arr) { remainders.add(num % m); } console.log(remainders.size);
Attempts:
2 left
💡 Hint
Calculate each remainder and count unique values.
✗ Incorrect
Remainders: 12%10=2, 25%10=5, 7%10=7, 14%10=4, 30%10=0. Unique: {0,2,4,5,7} — 5 distinct.
