Math and Number Theory Pattern: Template + 485 LeetCode Problems
Find the closed form, the invariant, or the modular identity — and skip the loop entirely.
- 121 Easy
- 244 Medium
- 120 Hard
- O(log n) or O(1) time
What the math and number theory pattern is
Some problems have no algorithm worth writing because they have a formula. The tell is a statement about digits, divisors, remainders, or counting arrangements, together with a constraint large enough — 10⁹ and up — that any loop over the input range is hopeless by construction. The recurring tools are small in number: the Euclidean algorithm for the greatest common divisor, which underlies fractions, cycles and repeating patterns; modular arithmetic, where addition and multiplication distribute over the modulus so you can reduce at every step instead of overflowing; a sieve when many primes below a bound are needed rather than one primality test; and fast exponentiation, which raises to the nth power in log n multiplications by squaring. The other half of this pattern is not a technique but a habit: compute the first six answers by hand, look for the pattern, then prove it. An invariant — a quantity the operations never change — usually collapses a simulation into a one-line check.
When to use it
- The constraints are far too large to iterate, so the answer must be computed rather than searched.
- The statement is about digits, divisibility, remainders, primes, or gcd.
- You are counting arrangements and the answer is requested modulo 10⁹ + 7.
- A simulation is described but some quantity is clearly conserved throughout it.
The math and number theory template in Python
The shape, not a solution to any one problem. Adapt the condition and the summary being maintained; the skeleton stays the same across the 485 problems listed below.
def gcd(a, b):
while b:
a, b = b, a % b # gcd(a, b) == gcd(b, a mod b), and b strictly shrinks
return a
def power_mod(base, exponent, mod):
result = 1
base %= mod
while exponent:
if exponent & 1: # this bit of the exponent is set
result = result * base % mod
base = base * base % mod # square for the next bit
exponent >>= 1
return resultComplexity characteristics
- Time
- O(log n) or O(1)
- Auxiliary space
- O(1)
The point of the pattern is that there is no loop over the input: a closed form is O(1), the Euclidean algorithm for a greatest common divisor is O(log min(a, b)), and fast exponentiation raises to the nth power in O(log n) multiplications. A sieve is the exception that costs real time and memory — O(n log log n) to build and O(n) to hold — and is only worth it when many primes below a bound are needed rather than a single primality test.
All 485 math and number theory LeetCode problems
Every problem in the library the math and number theory pattern applies to, grouped by LeetCode's own difficulty rating. 413 of the 485 carry a complete Python solution with a worked example and complexity analysis; the rest are listed for completeness, with the LeetCode Premium ones marked.
Easy (121)
Medium (244)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 2 | Add Two Numbers | Medium | Recursion, Linked List, Math |
| 7 | Reverse Integer | Medium | Math |
| 12 | Integer to Roman | Medium | Hash Table, Math, String |
| 29 | Divide Two Integers | Medium | Bit Manipulation, Math |
| 43 | Multiply Strings | Medium | Math, String, Simulation |
| 48 | Rotate Image | Medium | Array, Math, Matrix |
| 50 | Pow(x, n) | Medium | Recursion, Math |
| 62 | Unique Paths | Medium | Math, Dynamic Programming, Combinatorics |
| 89 | Gray Code | Medium | Bit Manipulation, Math, Backtracking |
| 96 | Unique Binary Search Trees | Medium | Tree, Binary Search Tree, Math +2 |
| 150 | Evaluate Reverse Polish Notation | Medium | Stack, Array, Math |
| 166 | Fraction to Recurring Decimal | Medium | Hash Table, Math, String |
| 172 | Factorial Trailing Zeroes | Medium | Math |
| 189 | Rotate Array | Medium | Array, Math, Two Pointers |
| 204 | Count Primes | Medium | Array, Math, Enumeration +1 |
| 223 | Rectangle Area | Medium | Geometry, Math |
| 227 | Basic Calculator II | Medium | Stack, Math, String |
| 241 | Different Ways to Add Parentheses | Medium | Recursion, Memoization, Math +2 |
| 264 | Ugly Number II | Medium | Hash Table, Math, Dynamic Programming +1 |
| 279 | Perfect Squares | Medium | Breadth-First Search, Math, Dynamic Programming |
| 313 | Super Ugly Number | Medium | Array, Math, Dynamic Programming |
| 319 | Bulb Switcher | Medium | Brainteaser, Math |
| 343 | Integer Break | Medium | Math, Dynamic Programming |
| 357 | Count Numbers with Unique Digits | Medium | Math, Dynamic Programming, Backtracking |
| 365 | Water and Jug Problem | Medium | Depth-First Search, Breadth-First Search, Math |
| 368 | Largest Divisible Subset | Medium | Array, Math, Dynamic Programming +1 |
| 371 | Sum of Two Integers | Medium | Bit Manipulation, Math |
| 372 | Super Pow | Medium | Math, Divide and Conquer |
| 375 | Guess Number Higher or Lower II | Medium | Math, Dynamic Programming, Game Theory |
| 380 | Insert Delete GetRandom O(1) | Medium | Design, Array, Hash Table +2 |
| 382 | Linked List Random Node | Medium | Reservoir Sampling, Linked List, Math +1 |
| 384 | Shuffle an Array | Medium | Design, Array, Math +1 |
| 390 | Elimination Game | Medium | Recursion, Math |
| 396 | Rotate Function | Medium | Array, Math, Dynamic Programming |
| 398 | Random Pick Index | Medium | Reservoir Sampling, Hash Table, Math +1 |
| 400 | Nth Digit | Medium | Math, Binary Search |
| 423 | Reconstruct Original Digits from English | Medium | Hash Table, Math, String |
| 445 | Add Two Numbers II | Medium | Stack, Linked List, Math |
| 447 | Number of Boomerangs | Medium | Array, Hash Table, Math |
| 453 | Minimum Moves to Equal Array Elements | Medium | Array, Math |
| 462 | Minimum Moves to Equal Array Elements II | Medium | Array, Math, Sorting |
| 464 | Can I Win | Medium | Bit Manipulation, Memoization, Math +3 |
| 470 | Implement Rand10() Using Rand7() | Medium | Math, Rejection Sampling, Probability and Statistics +1 |
| 477 | Total Hamming Distance | Medium | Bit Manipulation, Array, Math |
| 478 | Generate Random Point in a Circle | Medium | Geometry, Math, Rejection Sampling +1 |
| 486 | Predict the Winner | Medium | Recursion, Array, Math +2 |
| 497 | Random Point in Non-overlapping Rectangles | Medium | Reservoir Sampling, Array, Math +4 |
| 519 | Random Flip Matrix | Medium | Reservoir Sampling, Hash Table, Math +1 |
| 523 | Continuous Subarray Sum | Medium | Array, Hash Table, Math +1 |
| 528 | Random Pick with Weight | Medium | Array, Math, Binary Search +2 |
| 537 | Complex Number Multiplication | Medium | Math, String, Simulation |
| 539 | Minimum Time Difference | Medium | Array, Math, String +1 |
| 553 | Optimal Division | Medium | Array, Math, Dynamic Programming |
| 556 | Next Greater Element III | Medium | Math, Two Pointers, String |
| 592 | Fraction Addition and Subtraction | Medium | Math, String, Simulation |
| 593 | Valid Square | Medium | Geometry, Math |
| 633 | Sum of Square Numbers | Medium | Math, Two Pointers, Binary Search |
| 640 | Solve the Equation | Medium | Math, String, Simulation |
| 650 | 2 Keys Keyboard | Medium | Math, Dynamic Programming |
| 667 | Beautiful Arrangement II | Medium | Array, Math |
| 670 | Maximum Swap | Medium | Greedy, Math |
| 672 | Bulb Switcher II | Medium | Bit Manipulation, Depth-First Search, Breadth-First Search +1 |
| 738 | Monotone Increasing Digits | Medium | Greedy, Math |
| 973 | K Closest Points to Origin | Medium | Geometry, Array, Math +4 |
| 294 | Flip Game IIPremium | Medium | Memoization, Math, Dynamic Programming +2 |
| 356 | Line ReflectionPremium | Medium | Array, Hash Table, Math |
| 360 | Sort Transformed ArrayPremium | Medium | Array, Math, Two Pointers +1 |
| 369 | Plus One Linked ListPremium | Medium | Linked List, Math |
| 469 | Convex PolygonPremium | Medium | Geometry, Array, Math |
| 573 | Squirrel SimulationPremium | Medium | Array, Math |
| 625 | Minimum FactorizationPremium | Medium | Greedy, Math |
| 634 | Find the Derangement of An ArrayPremium | Medium | Math, Dynamic Programming, Combinatorics |
| 651 | 4 Keys KeyboardPremium | Medium | Math, Dynamic Programming |
| 750 | Number Of Corner RectanglesPremium | Medium | Array, Math, Dynamic Programming +1 |
| 754 | Reach a Number | Medium | Math, Binary Search |
| 775 | Global and Local Inversions | Medium | Array, Math |
| 779 | K-th Symbol in Grammar | Medium | Bit Manipulation, Recursion, Math |
| 781 | Rabbits in Forest | Medium | Greedy, Array, Hash Table +1 |
| 788 | Rotated Digits | Medium | Math, Dynamic Programming |
| 789 | Escape The Ghosts | Medium | Array, Math |
| 808 | Soup Servings | Medium | Math, Dynamic Programming, Probability and Statistics |
| 837 | New 21 Game | Medium | Math, Dynamic Programming, Sliding Window +1 |
| 840 | Magic Squares In Grid | Medium | Array, Hash Table, Math +1 |
| 858 | Mirror Reflection | Medium | Geometry, Math, Number Theory |
| 866 | Prime Palindrome | Medium | Math, Number Theory |
| 869 | Reordered Power of 2 | Medium | Hash Table, Math, Counting +2 |
| 877 | Stone Game | Medium | Array, Math, Dynamic Programming +1 |
| 910 | Smallest Range II | Medium | Greedy, Array, Math +1 |
| 932 | Beautiful Array | Medium | Array, Math, Divide and Conquer |
| 939 | Minimum Area Rectangle | Medium | Geometry, Array, Hash Table +2 |
| 957 | Prison Cells After N Days | Medium | Bit Manipulation, Array, Hash Table +1 |
| 963 | Minimum Area Rectangle II | Medium | Geometry, Array, Hash Table +1 |
| 970 | Powerful Integers | Medium | Hash Table, Math, Enumeration |
| 991 | Broken Calculator | Medium | Greedy, Math |
| 1006 | Clumsy Factorial | Medium | Stack, Math, Simulation |
| 1015 | Smallest Integer Divisible by K | Medium | Hash Table, Math |
| 1017 | Convert to Base -2 | Medium | Math |
| 1033 | Moving Stones Until Consecutive | Medium | Brainteaser, Math |
| 1040 | Moving Stones Until Consecutive II | Medium | Array, Math, Sorting +1 |
| 1041 | Robot Bounded In Circle | Medium | Math, String, Simulation |
| 1058 | Minimize Rounding Error to Meet TargetPremium | Medium | Greedy, Array, Math +2 |
| 1073 | Adding Two Negabinary Numbers | Medium | Array, Math |
| 1093 | Statistics from a Large Sample | Medium | Array, Math, Probability and Statistics |
| 1104 | Path In Zigzag Labelled Binary Tree | Medium | Tree, Math, Binary Tree |
| 1131 | Maximum of Absolute Value Expression | Medium | Array, Math |
| 1140 | Stone Game II | Medium | Array, Math, Dynamic Programming +2 |
| 1201 | Ugly Number III | Medium | Math, Binary Search, Combinatorics +1 |
| 1215 | Stepping NumbersPremium | Medium | Breadth-First Search, Math, Backtracking |
| 1227 | Airplane Seat Assignment Probability | Medium | Brainteaser, Math, Dynamic Programming +1 |
| 1230 | Toss Strange CoinsPremium | Medium | Array, Math, Dynamic Programming +1 |
| 1237 | Find Positive Integer Solution for a Given Equation | Medium | Math, Two Pointers, Binary Search +1 |
| 1238 | Circular Permutation in Binary Representation | Medium | Bit Manipulation, Math, Backtracking |
| 1247 | Minimum Swaps to Make Strings Equal | Medium | Greedy, Math, String |
| 1248 | Count Number of Nice Subarrays | Medium | Array, Hash Table, Math +2 |
| 1256 | Encode NumberPremium | Medium | Bit Manipulation, Math, String |
| 1276 | Number of Burgers with No Waste of Ingredients | Medium | Math |
| 1344 | Angle Between Hands of a Clock | Medium | Math |
| 1352 | Product of the Last K Numbers | Medium | Design, Array, Math +2 |
| 1362 | Closest Divisors | Medium | Math |
| 1390 | Four Divisors | Medium | Array, Math |
| 1401 | Circle and Rectangle Overlapping | Medium | Geometry, Math |
| 1414 | Find the Minimum Number of Fibonacci Numbers Whose Sum Is K | Medium | Greedy, Math |
| 1432 | Max Difference You Can Get From Changing an Integer | Medium | Greedy, Math |
| 1442 | Count Triplets That Can Form Two Arrays of Equal XOR | Medium | Bit Manipulation, Array, Hash Table +2 |
| 1447 | Simplified Fractions | Medium | Math, String, Number Theory |
| 1492 | The kth Factor of n | Medium | Math, Number Theory |
| 1513 | Number of Substrings With Only 1s | Medium | Math, String |
| 1524 | Number of Sub-arrays With Odd Sum | Medium | Array, Math, Dynamic Programming +1 |
| 1538 | Guess the Majority in a Hidden ArrayPremium | Medium | Array, Math, Interactive |
| 1551 | Minimum Operations to Make Array Equal | Medium | Math |
| 1561 | Maximum Number of Coins You Can Get | Medium | Greedy, Array, Math +2 |
| 1573 | Number of Ways to Split a String | Medium | Math, String |
| 1577 | Number of Ways Where Square of Number Is Equal to Product of Two Numbers | Medium | Array, Hash Table, Math +1 |
| 1621 | Number of Sets of K Non-Overlapping Line Segments | Medium | Math, Dynamic Programming, Combinatorics |
| 1628 | Design an Expression Tree With Evaluate FunctionPremium | Medium | Stack, Tree, Design +3 |
| 1634 | Add Two Polynomials Represented as Linked ListsPremium | Medium | Linked List, Math, Two Pointers |
| 1641 | Count Sorted Vowel Strings | Medium | Math, Dynamic Programming, Combinatorics |
| 1648 | Sell Diminishing-Valued Colored Balls | Medium | Greedy, Array, Math +3 |
| 1680 | Concatenation of Consecutive Binary Numbers | Medium | Bit Manipulation, Math, Simulation |
| 1685 | Sum of Absolute Differences in a Sorted Array | Medium | Array, Math, Prefix Sum |
| 1686 | Stone Game VI | Medium | Greedy, Array, Math +3 |
| 1690 | Stone Game VII | Medium | Array, Math, Dynamic Programming +1 |
| 1753 | Maximum Score From Removing Stones | Medium | Greedy, Math, Heap (Priority Queue) |
| 1759 | Count Number of Homogenous Substrings | Medium | Math, String |
| 1780 | Check if Number is a Sum of Powers of Three | Medium | Math |
| 1802 | Maximum Value at a Given Index in a Bounded Array | Medium | Greedy, Math, Binary Search |
| 1806 | Minimum Number of Operations to Reinitialize a Permutation | Medium | Array, Math, Simulation |
| 1814 | Count Nice Pairs in an Array | Medium | Array, Hash Table, Math +1 |
| 1823 | Find the Winner of the Circular Game | Medium | Recursion, Queue, Array +2 |
| 1828 | Queries on Number of Points Inside a Circle | Medium | Geometry, Array, Math |
| 1860 | Incremental Memory Leak | Medium | Math, Simulation |
| 1878 | Get Biggest Three Rhombus Sums in a Grid | Medium | Array, Math, Matrix +3 |
| 1884 | Egg Drop With 2 Eggs and N Floors | Medium | Math, Dynamic Programming |
| 1904 | The Number of Full Rounds You Have Played | Medium | Math, String |
| 1908 | Game of NimPremium | Medium | Bit Manipulation, Brainteaser, Array +3 |
| 1922 | Count Good Numbers | Medium | Recursion, Math |
| 1927 | Sum Game | Medium | Greedy, Math, String +1 |
| 1954 | Minimum Garden Perimeter to Collect Enough Apples | Medium | Math, Binary Search |
| 1969 | Minimum Non-Zero Product of the Array Elements | Medium | Greedy, Recursion, Math |
| 1999 | Smallest Greater Multiple Made of Two DigitsPremium | Medium | Math, Enumeration |
| 2001 | Number of Pairs of Interchangeable Rectangles | Medium | Array, Hash Table, Math +2 |
| 2028 | Find Missing Observations | Medium | Array, Math, Simulation |
| 2029 | Stone Game IX | Medium | Greedy, Array, Math +2 |
| 2033 | Minimum Operations to Make a Uni-Value Grid | Medium | Array, Math, Matrix +1 |
| 2038 | Remove Colored Pieces if Both Neighbors are the Same Color | Medium | Greedy, Math, String +1 |
| 2048 | Next Greater Numerically Balanced Number | Medium | Hash Table, Math, Backtracking +2 |
| 2063 | Vowels of All Substrings | Medium | Math, String, Dynamic Programming +1 |
| 2083 | Substrings That Begin and End With the Same LetterPremium | Medium | Hash Table, Math, String +2 |
| 2101 | Detonate the Maximum Bombs | Medium | Depth-First Search, Breadth-First Search, Graph +3 |
| 2110 | Number of Smooth Descent Periods of a Stock | Medium | Array, Math, Two Pointers +2 |
| 2125 | Number of Laser Beams in a Bank | Medium | Array, Math, String +1 |
| 2128 | Remove All Ones With Row and Column FlipsPremium | Medium | Bit Manipulation, Array, Math +1 |
| 2139 | Minimum Moves to Reach Target Score | Medium | Greedy, Math |
| 2152 | Minimum Number of Lines to Cover PointsPremium | Medium | Bit Manipulation, Geometry, Array +5 |
| 2162 | Minimum Cost to Set Cooking Time | Medium | Math, Enumeration |
| 2165 | Smallest Value of the Rearranged Number | Medium | Math, Sorting |
| 2177 | Find Three Consecutive Integers That Sum to a Given Number | Medium | Math, Simulation |
| 2178 | Maximum Split of Positive Even Integers | Medium | Greedy, Math, Backtracking |
| 2189 | Number of Ways to Build House of CardsPremium | Medium | Math, Dynamic Programming |
| 2195 | Append K Integers With Minimal Sum | Medium | Greedy, Array, Math +1 |
| 2198 | Number of Single Divisor TripletsPremium | Medium | Math |
| 2217 | Find Palindrome With Fixed Length | Medium | Array, Math |
| 2221 | Find Triangular Sum of an Array | Medium | Array, Math, Combinatorics +1 |
| 2240 | Number of Ways to Buy Pens and Pencils | Medium | Math, Enumeration |
| 2249 | Count Lattice Points Inside a Circle | Medium | Geometry, Array, Hash Table +2 |
| 2266 | Count Number of Texts | Medium | Hash Table, Math, String +1 |
| 2280 | Minimum Lines to Represent a Line Chart | Medium | Geometry, Array, Math +2 |
| 2310 | Sum of Numbers With Units Digit K | Medium | Greedy, Math, Dynamic Programming +1 |
| 2317 | Maximum XOR After Operations | Medium | Bit Manipulation, Array, Math |
| 2348 | Number of Zero-Filled Subarrays | Medium | Array, Math |
| 2358 | Maximum Number of Groups Entering a Competition | Medium | Greedy, Array, Math +1 |
| 2364 | Count Number of Bad Pairs | Medium | Array, Hash Table, Math +1 |
| 2393 | Count Strictly Increasing SubarraysPremium | Medium | Array, Math, Dynamic Programming |
| 2396 | Strictly Palindromic Number | Medium | Brainteaser, Math, Two Pointers |
| 2400 | Number of Ways to Reach a Position After Exactly k Steps | Medium | Math, Dynamic Programming, Combinatorics |
| 2417 | Closest Fair IntegerPremium | Medium | Math, Enumeration |
| 2436 | Minimum Split Into Subarrays With GCD Greater Than OnePremium | Medium | Greedy, Array, Math +2 |
| 2442 | Count Number of Distinct Integers After Reverse Operations | Medium | Array, Hash Table, Math +1 |
| 2443 | Sum of Number and Its Reverse | Medium | Math, Enumeration |
| 2447 | Number of Subarrays With GCD Equal to K | Medium | Array, Math, Number Theory |
| 2450 | Number of Distinct Binary Strings After Applying OperationsPremium | Medium | Math, String |
| 2457 | Minimum Addition to Make Integer Beautiful | Medium | Greedy, Math |
| 2464 | Minimum Subarrays in a Valid SplitPremium | Medium | Array, Math, Dynamic Programming +1 |
| 2470 | Number of Subarrays With LCM Equal to K | Medium | Array, Math, Number Theory |
| 2489 | Number of Substrings With Fixed RatioPremium | Medium | Hash Table, Math, String +1 |
| 2495 | Number of Subarrays Having Even ProductPremium | Medium | Array, Math, Dynamic Programming |
| 2505 | Bitwise OR of All Subsequence SumsPremium | Medium | Bit Manipulation, Brainteaser, Array +2 |
| 2507 | Smallest Value After Replacing With Sum of Prime Factors | Medium | Math, Number Theory, Simulation |
| 2513 | Minimize the Maximum of Two Arrays | Medium | Math, Binary Search, Number Theory |
| 2521 | Distinct Prime Factors of Product of Array | Medium | Array, Hash Table, Math +1 |
| 2523 | Closest Prime Numbers in Range | Medium | Math, Number Theory |
| 2527 | Find Xor-Beauty of Array | Medium | Bit Manipulation, Array, Math |
| 2539 | Count the Number of Good SubsequencesPremium | Medium | Hash Table, Math, String +2 |
| 2541 | Minimum Operations to Make Array Equal II | Medium | Greedy, Array, Math |
| 2550 | Count Collisions of Monkeys on a Polygon | Medium | Recursion, Math |
| 2572 | Count the Number of Square-Free Subsets | Medium | Bit Manipulation, Array, Math +2 |
| 2575 | Find the Divisibility Array of a String | Medium | Array, Math, String |
| 2579 | Count Total Number of Colored Cells | Medium | Math |
| 2597 | The Number of Beautiful Subsets | Medium | Array, Hash Table, Math +4 |
| 2598 | Smallest Missing Non-negative Integer After Operations | Medium | Greedy, Array, Hash Table +1 |
| 2601 | Prime Subtraction Operation | Medium | Greedy, Array, Math +2 |
| 2607 | Make K-Subarray Sums Equal | Medium | Greedy, Array, Math +2 |
| 2638 | Count the Number of K-Free SubsetsPremium | Medium | Array, Math, Dynamic Programming +2 |
| 2654 | Minimum Number of Operations to Make All Array Elements Equal to 1 | Medium | Array, Math, Number Theory |
| 2698 | Find the Punishment Number of an Integer | Medium | Math, Backtracking |
| 2745 | Construct the Longest New String | Medium | Greedy, Brainteaser, Math +1 |
| 2750 | Ways to Split Array Into Good Subarrays | Medium | Array, Math, Dynamic Programming |
| 2761 | Prime Pairs With Target Sum | Medium | Array, Math, Enumeration +1 |
| 2802 | Find The K-th Lucky NumberPremium | Medium | Bit Manipulation, Math, String |
| 2807 | Insert Greatest Common Divisors in Linked List | Medium | Linked List, Math, Number Theory |
| 2816 | Double a Number Represented as a Linked List | Medium | Stack, Linked List, Math |
| 2829 | Determine the Minimum Sum of a k-avoiding Array | Medium | Greedy, Math |
| 2834 | Find the Minimum Possible Sum of a Beautiful Array | Medium | Greedy, Math |
| 2844 | Minimum Operations to Make a Special Number | Medium | Greedy, Math, String +1 |
| 2847 | Smallest Number With Given Digit ProductPremium | Medium | Greedy, Math |
| 2849 | Determine if a Cell Is Reachable at a Given Time | Medium | Math |
| 2929 | Distribute Candies Among Children II | Medium | Math, Combinatorics, Enumeration |
| 2930 | Number of Strings Which Can Be Rearranged to Contain Substring | Medium | Math, Dynamic Programming, Combinatorics |
| 2939 | Maximum Xor Product | Medium | Greedy, Bit Manipulation, Math |
| 2947 | Count Beautiful Substrings I | Medium | Hash Table, Math, String +3 |
| 2961 | Double Modular Exponentiation | Medium | Array, Math, Simulation |
| 2967 | Minimum Cost to Make Array Equalindromic | Medium | Greedy, Array, Math +2 |
| 2979 | Most Expensive Item That Can Not Be BoughtPremium | Medium | Math, Dynamic Programming, Number Theory |
| 2992 | Number of Self-Divisible PermutationsPremium | Medium | Bit Manipulation, Array, Math +4 |
Hard (120)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 60 | Permutation Sequence | Hard | Recursion, Math |
| 149 | Max Points on a Line | Hard | Geometry, Array, Hash Table +1 |
| 224 | Basic Calculator | Hard | Stack, Recursion, Math +1 |
| 233 | Number of Digit One | Hard | Recursion, Math, Dynamic Programming |
| 273 | Integer to English Words | Hard | Recursion, Math, String |
| 282 | Expression Add Operators | Hard | Math, String, Backtracking |
| 335 | Self Crossing | Hard | Geometry, Array, Math |
| 381 | Insert Delete GetRandom O(1) - Duplicates allowed | Hard | Design, Array, Hash Table +2 |
| 391 | Perfect Rectangle | Hard | Geometry, Array, Hash Table +2 |
| 458 | Poor Pigs | Hard | Math, Dynamic Programming, Combinatorics |
| 479 | Largest Palindrome Product | Hard | Math, Enumeration |
| 483 | Smallest Good Base | Hard | Math, Binary Search |
| 564 | Find the Closest Palindrome | Hard | Math, String |
| 587 | Erect the Fence | Hard | Geometry, Array, Math |
| 668 | Kth Smallest Number in Multiplication Table | Hard | Math, Binary Search |
| 679 | 24 Game | Hard | Array, Math, Backtracking |
| 710 | Random Pick with Blacklist | Hard | Array, Hash Table, Math +3 |
| 296 | Best Meeting PointPremium | Hard | Array, Math, Matrix +1 |
| 660 | Remove 9Premium | Hard | Math |
| 770 | Basic Calculator IV | Hard | Stack, Recursion, Hash Table +2 |
| 772 | Basic Calculator IIIPremium | Hard | Stack, Recursion, Math +1 |
| 780 | Reaching Points | Hard | Math |
| 782 | Transform to Chessboard | Hard | Bit Manipulation, Array, Math +1 |
| 793 | Preimage Size of Factorial Zeroes Function | Hard | Math, Binary Search |
| 805 | Split Array With Same Average | Hard | Bit Manipulation, Array, Math +2 |
| 810 | Chalkboard XOR Game | Hard | Bit Manipulation, Brainteaser, Array +2 |
| 829 | Consecutive Numbers Sum | Hard | Math, Enumeration |
| 843 | Guess the Word | Hard | Array, Math, String +2 |
| 878 | Nth Magical Number | Hard | Math, Binary Search |
| 887 | Super Egg Drop | Hard | Math, Binary Search, Dynamic Programming |
| 891 | Sum of Subsequence Widths | Hard | Array, Math, Sorting |
| 899 | Orderly Queue | Hard | Math, String, Sorting |
| 902 | Numbers At Most N Given Digit Set | Hard | Array, Math, String +2 |
| 906 | Super Palindromes | Hard | Math, String, Enumeration |
| 913 | Cat and Mouse | Hard | Graph, Topological Sort, Memoization +3 |
| 920 | Number of Music Playlists | Hard | Math, Dynamic Programming, Combinatorics |
| 927 | Three Equal Parts | Hard | Array, Math |
| 952 | Largest Component Size by Common Factor | Hard | Union Find, Array, Hash Table +2 |
| 964 | Least Operators to Express Number | Hard | Memoization, Math, Dynamic Programming |
| 972 | Equal Rational Numbers | Hard | Math, String |
| 996 | Number of Squareful Arrays | Hard | Bit Manipulation, Array, Hash Table +4 |
| 1012 | Numbers With Repeated Digits | Hard | Math, Dynamic Programming |
| 1067 | Digit Count in RangePremium | Hard | Math, Dynamic Programming |
| 1088 | Confusing Number IIPremium | Hard | Math, Backtracking |
| 1183 | Maximum Number of OnesPremium | Hard | Greedy, Math, Sorting +1 |
| 1199 | Minimum Time to Build BlocksPremium | Hard | Greedy, Array, Math +1 |
| 1250 | Check If It Is a Good Array | Hard | Array, Math, Number Theory |
| 1259 | Handshakes That Don't CrossPremium | Hard | Math, Dynamic Programming |
| 1307 | Verbal Arithmetic Puzzle | Hard | Array, Math, String +1 |
| 1330 | Reverse Subarray To Maximize Array Value | Hard | Greedy, Array, Math |
| 1359 | Count All Valid Pickup and Delivery Options | Hard | Math, Dynamic Programming, Combinatorics |
| 1363 | Largest Multiple of Three | Hard | Greedy, Array, Math +2 |
| 1406 | Stone Game III | Hard | Array, Math, Dynamic Programming +1 |
| 1453 | Maximum Number of Darts Inside of a Circular Dartboard | Hard | Geometry, Array, Math |
| 1467 | Probability of a Two Boxes Having The Same Number of Distinct Balls | Hard | Array, Math, Dynamic Programming +3 |
| 1478 | Allocate Mailboxes | Hard | Array, Math, Dynamic Programming +1 |
| 1510 | Stone Game IV | Hard | Math, Dynamic Programming, Game Theory |
| 1515 | Best Position for a Service Centre | Hard | Geometry, Array, Math +1 |
| 1563 | Stone Game V | Hard | Array, Math, Dynamic Programming +1 |
| 1569 | Number of Ways to Reorder Array to Get Same BST | Hard | Tree, Union Find, Binary Search Tree +7 |
| 1610 | Maximum Number of Visible Points | Hard | Geometry, Array, Math +2 |
| 1622 | Fancy Sequence | Hard | Design, Segment Tree, Math |
| 1627 | Graph Connectivity With Threshold | Hard | Union Find, Array, Math +1 |
| 1643 | Kth Smallest Instructions | Hard | Array, Math, Dynamic Programming +1 |
| 1728 | Cat and Mouse II | Hard | Graph, Topological Sort, Memoization +5 |
| 1735 | Count Ways to Make Array With Product | Hard | Array, Math, Dynamic Programming +2 |
| 1739 | Building Boxes | Hard | Greedy, Math, Binary Search |
| 1766 | Tree of Coprimes | Hard | Tree, Depth-First Search, Array +2 |
| 1776 | Car Fleet II | Hard | Stack, Array, Math +2 |
| 1799 | Maximize Score After N Operations | Hard | Bit Manipulation, Array, Math +4 |
| 1808 | Maximize Number of Nice Divisors | Hard | Recursion, Math, Number Theory |
| 1819 | Number of Different Subsequences GCDs | Hard | Array, Math, Counting +1 |
| 1830 | Minimum Number of Operations to Make String Sorted | Hard | Math, String, Combinatorics |
| 1835 | Find XOR Sum of All Pairs Bitwise AND | Hard | Bit Manipulation, Array, Math |
| 1840 | Maximum Building Height | Hard | Array, Math, Sorting |
| 1862 | Sum of Floored Pairs | Hard | Array, Math, Binary Search +1 |
| 1866 | Number of Ways to Rearrange Sticks With K Sticks Visible | Hard | Math, Dynamic Programming, Combinatorics |
| 1872 | Stone Game VIII | Hard | Array, Math, Dynamic Programming +2 |
| 1896 | Minimum Cost to Change the Final Value of Expression | Hard | Stack, Math, String +1 |
| 1916 | Count Ways to Build Rooms in an Ant Colony | Hard | Tree, Graph, Topological Sort +3 |
| 1924 | Erect the Fence IIPremium | Hard | Geometry, Array, Math |
| 1956 | Minimum Time For K Virus Variants to SpreadPremium | Hard | Geometry, Array, Math +2 |
| 1994 | The Number of Good Subsets | Hard | Bit Manipulation, Array, Hash Table +5 |
| 1998 | GCD Sort of an Array | Hard | Union Find, Array, Math +2 |
| 2005 | Subtree Removal Game with Fibonacci TreePremium | Hard | Tree, Math, Dynamic Programming +2 |
| 2019 | The Score of Students Solving Math Expression | Hard | Stack, Memoization, Array +4 |
| 2081 | Sum of k-Mirror Numbers | Hard | Math, Enumeration |
| 2117 | Abbreviating the Product of a Range | Hard | Math |
| 2147 | Number of Ways to Divide a Long Corridor | Hard | Math, String, Dynamic Programming |
| 2183 | Count Array Pairs Divisible by K | Hard | Array, Math, Number Theory |
| 2197 | Replace Non-Coprime Numbers in Array | Hard | Stack, Array, Math +1 |
| 2338 | Count the Number of Ideal Arrays | Hard | Math, Dynamic Programming, Combinatorics +1 |
| 2344 | Minimum Deletions to Make Array Divisible | Hard | Array, Math, Number Theory +2 |
| 2366 | Minimum Replacements to Sort the Array | Hard | Greedy, Array, Math |
| 2376 | Count Special Integers | Hard | Math, Dynamic Programming |
| 2440 | Create Components With Same Value | Hard | Tree, Depth-First Search, Array +2 |
| 2514 | Count Anagrams | Hard | Hash Table, Math, String +2 |
| 2524 | Maximum Frequency Score of a SubarrayPremium | Hard | Stack, Array, Hash Table +2 |
| 2543 | Check if Point Is Reachable | Hard | Math, Number Theory |
| 2584 | Split the Array to Make Coprime Products | Hard | Array, Hash Table, Math +1 |
| 2613 | Beautiful PairsPremium | Hard | Geometry, Array, Math +3 |
| 2647 | Color the Triangle RedPremium | Hard | Array, Math |
| 2681 | Power of Heroes | Hard | Array, Math, Dynamic Programming +2 |
| 2709 | Greatest Common Divisor Traversal | Hard | Union Find, Array, Math +1 |
| 2719 | Count of Integers | Hard | Math, String, Dynamic Programming |
| 2790 | Maximum Number of Groups With Increasing Length | Hard | Greedy, Array, Math +2 |
| 2818 | Apply Operations to Maximize Score | Hard | Stack, Greedy, Array +4 |
| 2827 | Number of Beautiful Integers in the Range | Hard | Math, Dynamic Programming |
| 2842 | Count K-Subsequences of a String With Maximum Beauty | Hard | Greedy, Hash Table, Math +2 |
| 2851 | String Transformation | Hard | Math, String, Dynamic Programming +1 |
| 2862 | Maximum Element-Sum of a Complete Subset of Indices | Hard | Array, Math, Number Theory |
| 2867 | Count Valid Paths in a Tree | Hard | Tree, Depth-First Search, Math +2 |
| 2868 | The Wording GamePremium | Hard | Greedy, Array, Math +3 |
| 2912 | Number of Ways to Reach Destination in the GridPremium | Hard | Math, Dynamic Programming, Combinatorics |
| 2927 | Distribute Candies Among Children IIIPremium | Hard | Math, Combinatorics |
| 2941 | Maximum GCD-Sum of a SubarrayPremium | Hard | Array, Math, Binary Search +1 |
| 2949 | Count Beautiful Substrings II | Hard | Hash Table, Math, String +2 |
| 2954 | Count the Number of Infection Sequences | Hard | Array, Math, Combinatorics |
| 2963 | Count the Number of Good Partitions | Hard | Array, Hash Table, Math +1 |
| 2999 | Count the Number of Powerful Integers | Hard | Math, String, Dynamic Programming |
Related patterns
Problems sit in more than one pattern more often than not, and the overlap is where the interesting follow-up questions live.
Math and Number Theory pattern FAQ
What is the math and number theory pattern?
Some problems have no algorithm worth writing because they have a formula. The tell is a statement about digits, divisors, remainders, or counting arrangements, together with a constraint large enough — 10⁹ and up — that any loop over the input range is hopeless by construction.
How many LeetCode problems use the math and number theory pattern?
This page lists 485 LeetCode problems that the math and number theory pattern applies to: 121 Easy, 244 Medium and 120 Hard. 413 of them carry a complete Python solution with complexity analysis.
What is the time complexity of the math and number theory pattern?
O(log n) or O(1) time and O(1) space. The point of the pattern is that there is no loop over the input: a closed form is O(1), the Euclidean algorithm for a greatest common divisor is O(log min(a, b)), and fast exponentiation raises to the nth power in O(log n) multiplications. A sieve is the exception that costs real time and memory — O(n log log n) to build and O(n) to hold — and is only worth it when many primes below a bound are needed rather than a single primality test.
When should I use the math and number theory pattern in an interview?
The constraints are far too large to iterate, so the answer must be computed rather than searched. The statement is about digits, divisibility, remainders, primes, or gcd.
Which math and number theory problem should I start with?
LeetCode 9. Palindrome Number is the lowest-numbered Easy problem on this page, which makes it the usual starting point: the technique is visible without the problem's own complications getting in the way.
What patterns are related to math and number theory?
Bit Manipulation, Dynamic Programming, Greedy, Prefix Sum. Problems frequently sit in more than one of these, and the overlap is where the interesting follow-up questions come from.
More ways in: all 22 patterns, the curated study lists, or the full problem list.
Meet the math and number theory problem you did not practise
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.