Skip to main content

➗ Math

One-line summary: Number theory, combinatorics, and arithmetic tricks — the foundation of competitive programming warm-ups.


Key Topics

TopicKey Insight
GCD / LCMEuclidean: gcd(a,b) = gcd(b, a%b)
Sieve of EratosthenesFind all primes ≤ n in O(n log log n)
Modular Arithmetic(a+b)%m = ((a%m)+(b%m))%m
CombinatoricsnCr = n! / (r! * (n-r)!)
Trailing zeros in n!Count factor-5s: ⌊n/5⌋ + ⌊n/25⌋ + ...
Perfect squareMath.sqrt(n) % 1 === 0
Fibonacci

Common Patterns

GCD / LCM

function gcd(a, b) {
return b === 0 ? a : gcd(b, a % b);
}
function lcm(a, b) {
return (a / gcd(a, b)) * b;
}

Sieve of Eratosthenes

function sieve(n) {
const p = new Array(n + 1).fill(true);
p[0] = p[1] = false;
for (let i = 2; i * i <= n; i++) if (p[i]) for (let j = i * i; j <= n; j += i) p[j] = false;
return p;
}

Trailing Zeros

function trailingZeros(n) {
let c = 0;
while (n >= 5) {
n = Math.floor(n / 5);
c += n;
}
return c;
}

Practice Problems

Easy Problems

ProblemDifficultySolution
LC 7 — Reverse IntegerEasy
LC 9 — Palindrome NumberEasy
LC 13 — Roman to IntegerEasy
LC 66 — Plus OneEasy
LC 69 — Sqrt(x)Easy
LC 168 — Excel Sheet Column TitleEasy
LC 171 — Excel Sheet Column NumberEasy
LC 202 — Happy NumberEasy
LC 231 — Power of TwoEasy
LC 258 — Add DigitsEasy
LC 263 — Ugly NumberEasy
LC 326 — Power of ThreeEasy
LC 342 — Power of FourEasy
LC 367 — Valid Perfect SquareEasy
LC 415 — Add StringsEasy
LC 441 — Arranging CoinsEasy
LC 507 — Perfect NumberEasy
LC 509 — Fibonacci NumberEasyView Solution
LC 728 — Self Dividing NumbersEasy
LC 1137 — N-th Tribonacci NumberEasy
LC 1175 — Prime ArrangementsEasy
LC 1281 — Subtract the Product and Sum of DigitsEasy
LC 1317 — Convert Integer to the Sum of Two No-Zero IntegersEasy
LC 1323 — Maximum 69 NumberEasy
LC 1360 — Number of Days Between Two DatesEasy
CC — Add Two Numbers (FLOW001)Easy
CC — Small Factorials (FLOW018)Easy
CC — First and Last Digit (FLOW004)Easy
CC — Sum of Digits (FLOW006)Easy
CC — GCD and LCM (FLOW016)Easy
CC — Prime Generator (PRIME1)Easy
CC — Reverse Number (REVNUM)Easy
LC 12 — Integer to RomanMedium
LC 29 — Divide Two IntegersMedium
LC 43 — Multiply StringsMedium
LC 50 — Pow(x, n)Medium
LC 166 — Fraction to Recurring DecimalMedium
LC 172 — Factorial Trailing ZeroesMedium
LC 204 — Count PrimesMedium
LC 264 — Ugly Number IIMedium
LC 279 — Perfect SquaresMedium
LC 319 — Bulb SwitcherMedium
LC 365 — Water and Jug ProblemMedium
LC 372 — Super PowMedium
LC 396 — Rotate FunctionMedium
LC 470 — Implement Rand10 Using Rand7Medium
LC 593 — Valid SquareMedium
LC 633 — Sum of Square NumbersMedium
LC 754 — Reach a NumberMedium
LC 780 — Reaching PointsMedium
LC 829 — Consecutive Numbers SumMedium
LC 858 — Mirror ReflectionMedium
LC 914 — X of a Kind in a Deck of CardsMedium
LC 1006 — Clumsy FactorialMedium
LC 1217 — Minimum Cost to Move Chips to The Same PositionMedium
LC 1390 — Four DivisorsMedium
CC — Modular Exponentiation (MODEX)Medium
CC — Catalan Numbers (CATALAN)Medium
CC — Number Theory (NUMTHEORY)Medium
LC 60 — Permutation SequenceHard
LC 149 — Max Points on a LineHard
LC 233 — Number of Digit OneHard
LC 335 — Self CrossingHard
LC 391 — Perfect RectangleHard
LC 458 — Poor PigsHard
LC 564 — Find the Closest PalindromeHard
LC 587 — Erect the FenceHard
LC 679 — 24 GameHard
LC 753 — Cracking the SafeHard
LC 810 — Chalkboard XOR GameHard
LC 887 — Super Egg DropHard
LC 906 — Super PalindromesHard
LC 1012 — Numbers With Repeated DigitsHard
LC 1088 — Confusing Number IIHard
LC 1363 — Largest Multiple of ThreeHard
LC 1397 — Find All Good StringsHard
CC — Advanced Number Theory (ADVNUM)Hard
CC — Chinese Remainder Theorem (CRT)Hard
CC — Extended Euclidean (EXTEUC)Hard

← Back to Home · © sparshjaswal