Plus One — Carry Propagation Array Trick [Easy Interview Problem]
Increment a large integer represented as an array by one. Learn the carry propagation pattern with O(n) solutions in C, C++, Java, JavaScript and Python.
webcoderspeed.com
34 articles
Increment a large integer represented as an array by one. Learn the carry propagation pattern with O(n) solutions in C, C++, Java, JavaScript and Python.
Count the number of prime numbers strictly less than n. Master the Sieve of Eratosthenes — one of the most elegant algorithms in CS — with full code in C, C++, Java, JavaScript and Python.
Find the missing number in [0..n] using the Gauss sum formula or XOR in O(n) time O(1) space.
Find the minimum total moves to make all array elements equal by targeting the median value.
Find the maximum number of points on the same line using a slope-as-fraction HashMap for each anchor point.
Compute integer square root using right-boundary binary search to find the largest k where k*k <= x.
Maximize nums[index] given array length n, sum constraint maxSum, and each element >= 1 using binary search on the peak value.
Check if a number is a perfect square in O(log n) using binary search or by exploiting odd-number sum identity.
Find the minimum train speed to arrive on time given n rides (last ride doesn't wait) using binary search on speed.
Determine if a number is happy by repeatedly summing digit squares and detecting cycles using a HashSet or Floyd algorithm.
Convert a fraction to decimal string with recurring part detected via remainder position tracking.
Evaluate an RPN expression by pushing numbers and applying operators to the top two stack elements.
Calculate the score of balanced parentheses where () = 1 and AB = A+B and (A) = 2*A using a stack depth trick.
Complete reference for math and number theory DSA patterns: algorithm selection guide, complexity table, and top 25 interview problems.
Apply the inclusion-exclusion principle to count elements satisfying union of conditions. Solves divisibility, coverage, and derangement problems.
Master randomized DSA algorithms: reservoir sampling for streams, quickselect for O(n) kth element, and random shuffling.
Master combinatorial game theory: Nim XOR strategy, Grundy (nimber) values, and Sprague-Grundy theorem for composite games.
Tackle geometry problems in coding interviews: cross product, point-in-polygon, line intersection, and Graham scan convex hull.
Apply probability theory and expected value DP to competitive programming: dice problems, random walks, and geometric distribution.
Prevent integer overflow in competitive programming: safe multiplication, __int128, binary search on answers, and floating-point gotchas.
Build range query structures in O(sqrt n) per query with block decomposition. Simpler alternative to segment trees.
Solve systems of modular congruences with the Chinese Remainder Theorem. Fundamental for cryptography and competitive math.
Explore classic number sequences: Fibonacci, Lucas, Pell, tribonacci, with DP and matrix approaches.
Master bitwise operations for DSA: XOR tricks, Brian Kernighan bit counting, subset enumeration, and bitmask DP.
Solve linear recurrences like Fibonacci in O(log n) using matrix exponentiation. Essential for DP optimization on large n.
Explore digit manipulation, perfect/abundant numbers, Armstrong numbers, and common math interview patterns.
Compute combinations nCr efficiently with precomputed factorials, Pascal's triangle for small n, and Catalan numbers for tree/parenthesis counting.
Compute Euler's totient phi(n) for cryptography and modular inverse applications. Sieve variant for all values up to n.
Factor integers efficiently with trial division O(sqrt n), SPF sieve O(log n), and understand when each approach is optimal.
Master modular arithmetic for competitive programming: binary exponentiation O(log n), modular inverse, and Chinese Remainder Theorem.
Master GCD/LCM with Euclidean algorithm O(log n) and the Extended Euclidean for modular inverse computation.
Generate all primes up to n in O(n log log n) with the Sieve of Eratosthenes. Includes segmented sieve and prime factorization variants.
Master mathematical algorithms for DSA: primes, GCD, modular arithmetic, combinatorics, and fast exponentiation with 5-language implementations.
How to derive the OLS Estimator with matrix notation and a tour of math typesetting using markdown with the help of KaTeX.