Number Theory LeetCode Problems: All 54, With Python Solutions
Every problem in this library that LeetCode tags Number Theory — 54 in total, 47 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 54 problems
- 8 Easy
- 24 Medium
- 22 Hard
How Number Theory problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Number Theory problems — each one explains the approach, gives a Python template and states its complexity.
- Math and Number Theory — Find the closed form, the invariant, or the modular identity — and skip the loop entirely.
Number Theory problems by difficulty
Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (8)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 258 | Add Digits | Easy | Math, Number Theory, Simulation |
| 914 | X of a Kind in a Deck of Cards | Easy | Array, Hash Table, Math +2 |
| 1952 | Three Divisors | Easy | Math, Enumeration, Number Theory |
| 1979 | Find Greatest Common Divisor of Array | Easy | Array, Math, Number Theory |
| 2413 | Smallest Even Multiple | Easy | Math, Number Theory |
| 2427 | Number of Common Factors | Easy | Math, Enumeration, Number Theory |
| 2614 | Prime In Diagonal | Easy | Array, Math, Matrix +1 |
| 2748 | Number of Beautiful Pairs | Easy | Array, Hash Table, Math +2 |
Medium (24)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 204 | Count Primes | Medium | Array, Math, Enumeration +1 |
| 858 | Mirror Reflection | Medium | Geometry, Math, Number Theory |
| 866 | Prime Palindrome | Medium | Math, Number Theory |
| 1201 | Ugly Number III | Medium | Math, Binary Search, Combinatorics +1 |
| 1447 | Simplified Fractions | Medium | Math, String, Number Theory |
| 1492 | The kth Factor of n | Medium | Math, Number Theory |
| 2001 | Number of Pairs of Interchangeable Rectangles | Medium | Array, Hash Table, Math +2 |
| 2280 | Minimum Lines to Represent a Line Chart | Medium | Geometry, Array, Math +2 |
| 2447 | Number of Subarrays With GCD Equal to K | Medium | Array, Math, Number Theory |
| 2470 | Number of Subarrays With LCM Equal to K | Medium | Array, Math, Number Theory |
| 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 |
| 2601 | Prime Subtraction Operation | Medium | Greedy, Array, Math +2 |
| 2607 | Make K-Subarray Sums Equal | Medium | Greedy, Array, Math +2 |
| 2654 | Minimum Number of Operations to Make All Array Elements Equal to 1 | Medium | Array, Math, Number Theory |
| 2761 | Prime Pairs With Target Sum | Medium | Array, Math, Enumeration +1 |
| 2807 | Insert Greatest Common Divisors in Linked List | Medium | Linked List, Math, Number Theory |
| 2947 | Count Beautiful Substrings I | Medium | Hash Table, Math, String +3 |
| 2436 | Minimum Split Into Subarrays With GCD Greater Than OnePremium | Medium | Greedy, Array, Math +2 |
| 2464 | Minimum Subarrays in a Valid SplitPremium | Medium | Array, Math, Dynamic Programming +1 |
| 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 (22)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 952 | Largest Component Size by Common Factor | Hard | Union Find, Array, Hash Table +2 |
| 1250 | Check If It Is a Good Array | Hard | Array, Math, Number Theory |
| 1627 | Graph Connectivity With Threshold | Hard | Union Find, Array, Math +1 |
| 1735 | Count Ways to Make Array With Product | Hard | Array, Math, Dynamic Programming +2 |
| 1766 | Tree of Coprimes | Hard | Tree, Depth-First Search, Array +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 |
| 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 |
| 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 |
| 2543 | Check if Point Is Reachable | Hard | Math, Number Theory |
| 2584 | Split the Array to Make Coprime Products | Hard | Array, Hash Table, Math +1 |
| 2709 | Greatest Common Divisor Traversal | Hard | Union Find, Array, Math +1 |
| 2818 | Apply Operations to Maximize Score | Hard | Stack, Greedy, Array +4 |
| 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 |
| 2183 | Count Array Pairs Divisible by K | Hard | Array, Math, Number Theory |
| 2941 | Maximum GCD-Sum of a SubarrayPremium | Hard | Array, Math, Binary Search +1 |
| 2949 | Count Beautiful Substrings II | Hard | Hash Table, Math, String +2 |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Greedy346
- Depth-First Search289
- Binary Search253
- Database249
- Tree225
- Breadth-First Search223
- Matrix216
- Two Pointers201
- Bit Manipulation194
- Binary Tree174
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
When the Number Theory problem arrives live
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.