Modular Arithmetic Basics in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to perform modular arithmetic operations in code.
Specifically, how the time grows when we do many modular calculations.
Analyze the time complexity of the following code snippet.
function modAdd(a, b, m) {
return (a + b) % m;
}
function modMultiply(a, b, m) {
return (a * b) % m;
}
// Using modAdd and modMultiply in a loop
function sumModArray(arr, m) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum = modAdd(sum, arr[i], m);
}
return sum;
}
This code performs modular addition and multiplication, then sums an array with modular addition.
- Primary operation: The loop in
sumModArraythat adds each element modulom. - How many times: The loop runs once for each element in the array, so
ntimes wherenis the array length.
Each element requires one modular addition operation, so the total work grows directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 modular additions |
| 100 | 100 modular additions |
| 1000 | 1000 modular additions |
Pattern observation: The operations increase linearly as input size increases.
Time Complexity: O(n)
This means the time to complete the modular sum grows in direct proportion to the number of elements.
[X] Wrong: "Modular operations take more time as numbers get bigger, so complexity depends on number size too."
[OK] Correct: Modular arithmetic operations are constant time for fixed-size numbers in typical programming languages, so complexity depends mainly on how many operations we do, not the size of the numbers.
Understanding modular arithmetic time complexity helps you reason about algorithms that use it, like hashing or cryptography, showing you can analyze how your code scales.
"What if we replaced the loop with a recursive function that sums the array modulo m? How would the time complexity change?"
Practice
7 % 3 in modular arithmetic?Solution
Step 1: Understand the modulus operation
The modulus operator (%) gives the remainder after division of the first number by the second.Step 2: Calculate 7 divided by 3
7 divided by 3 is 2 with a remainder of 1.Final Answer:
1 -> Option CQuick Check:
7 % 3 = 1 [OK]
- Confusing modulus with division
- Using 3 instead of remainder
- Thinking modulus returns quotient
a divided by b in JavaScript?Solution
Step 1: Recall JavaScript modulus syntax
In JavaScript, the modulus operator is the percent sign (%) placed between two numbers or variables.Step 2: Identify correct syntax
Onlya % bcorrectly computes the remainder ofadivided byb.Final Answer:
a % b -> Option AQuick Check:
Use % for modulus in JavaScript [OK]
- Using 'mod' keyword
- Using integer division operator //
- Calling a non-existent mod() function
const x = 15; const y = 4; console.log(x % y);
Solution
Step 1: Calculate 15 divided by 4
15 divided by 4 is 3 with a remainder of 3.Step 2: Apply modulus operator
The expression15 % 4returns the remainder, which is 3.Final Answer:
3 -> Option BQuick Check:
15 % 4 = 3 [OK]
- Confusing remainder with quotient
- Expecting 4 instead of remainder
- Mistaking modulus for division
let a = 10; let b = 3; let result = a % b(); console.log(result);
Solution
Step 1: Analyze the modulus expression
The code usesa % b(), which treatsbas a function call, butbis a number, not a function.Step 2: Identify the error
Parentheses afterbcause a runtime error because numbers cannot be called like functions.Final Answer:
Using parentheses after b causes an error -> Option DQuick Check:
Variables are not functions, so no () after b [OK]
- Adding () after number variables
- Confusing modulus with division
- Ignoring runtime errors
a = 123456789 and b = 987654321 is divided by 1000. Which expression correctly computes this using modular arithmetic properties?Solution
Step 1: Recall modular addition property
The remainder of a sum modulo m equals the sum of remainders modulo m: (a + b) % m = ((a % m) + (b % m)) % m.Step 2: Apply property to given numbers
Compute(a % 1000)and(b % 1000), add them, then take modulo 1000 again to get correct remainder.Final Answer:
((a % 1000) + (b % 1000)) % 1000 -> Option AQuick Check:
Use modular addition: ((a%m)+(b%m))%m [OK]
- Skipping final modulus after addition
- Adding without modulus
- Using wrong modulus order
Ready to prove it?
Pass the test and this pattern moves unknown
