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)

#ProblemDifficultyTopics
258Add DigitsEasyMath, Number Theory, Simulation
914X of a Kind in a Deck of CardsEasyArray, Hash Table, Math +2
1952Three DivisorsEasyMath, Enumeration, Number Theory
1979Find Greatest Common Divisor of ArrayEasyArray, Math, Number Theory
2413Smallest Even MultipleEasyMath, Number Theory
2427Number of Common FactorsEasyMath, Enumeration, Number Theory
2614Prime In DiagonalEasyArray, Math, Matrix +1
2748Number of Beautiful PairsEasyArray, Hash Table, Math +2

Medium (24)

#ProblemDifficultyTopics
204Count PrimesMediumArray, Math, Enumeration +1
858Mirror ReflectionMediumGeometry, Math, Number Theory
866Prime PalindromeMediumMath, Number Theory
1201Ugly Number IIIMediumMath, Binary Search, Combinatorics +1
1447Simplified FractionsMediumMath, String, Number Theory
1492The kth Factor of nMediumMath, Number Theory
2001Number of Pairs of Interchangeable RectanglesMediumArray, Hash Table, Math +2
2280Minimum Lines to Represent a Line ChartMediumGeometry, Array, Math +2
2447Number of Subarrays With GCD Equal to KMediumArray, Math, Number Theory
2470Number of Subarrays With LCM Equal to KMediumArray, Math, Number Theory
2507Smallest Value After Replacing With Sum of Prime FactorsMediumMath, Number Theory, Simulation
2513Minimize the Maximum of Two ArraysMediumMath, Binary Search, Number Theory
2521Distinct Prime Factors of Product of ArrayMediumArray, Hash Table, Math +1
2523Closest Prime Numbers in RangeMediumMath, Number Theory
2601Prime Subtraction OperationMediumGreedy, Array, Math +2
2607Make K-Subarray Sums EqualMediumGreedy, Array, Math +2
2654Minimum Number of Operations to Make All Array Elements Equal to 1MediumArray, Math, Number Theory
2761Prime Pairs With Target SumMediumArray, Math, Enumeration +1
2807Insert Greatest Common Divisors in Linked ListMediumLinked List, Math, Number Theory
2947Count Beautiful Substrings IMediumHash Table, Math, String +3
2436Minimum Split Into Subarrays With GCD Greater Than OnePremiumMediumGreedy, Array, Math +2
2464Minimum Subarrays in a Valid SplitPremiumMediumArray, Math, Dynamic Programming +1
2979Most Expensive Item That Can Not Be BoughtPremiumMediumMath, Dynamic Programming, Number Theory
2992Number of Self-Divisible PermutationsPremiumMediumBit Manipulation, Array, Math +4

Hard (22)

#ProblemDifficultyTopics
952Largest Component Size by Common FactorHardUnion Find, Array, Hash Table +2
1250Check If It Is a Good ArrayHardArray, Math, Number Theory
1627Graph Connectivity With ThresholdHardUnion Find, Array, Math +1
1735Count Ways to Make Array With ProductHardArray, Math, Dynamic Programming +2
1766Tree of CoprimesHardTree, Depth-First Search, Array +2
1799Maximize Score After N OperationsHardBit Manipulation, Array, Math +4
1808Maximize Number of Nice DivisorsHardRecursion, Math, Number Theory
1819Number of Different Subsequences GCDsHardArray, Math, Counting +1
1994The Number of Good SubsetsHardBit Manipulation, Array, Hash Table +5
1998GCD Sort of an ArrayHardUnion Find, Array, Math +2
2197Replace Non-Coprime Numbers in ArrayHardStack, Array, Math +1
2338Count the Number of Ideal ArraysHardMath, Dynamic Programming, Combinatorics +1
2344Minimum Deletions to Make Array DivisibleHardArray, Math, Number Theory +2
2543Check if Point Is ReachableHardMath, Number Theory
2584Split the Array to Make Coprime ProductsHardArray, Hash Table, Math +1
2709Greatest Common Divisor TraversalHardUnion Find, Array, Math +1
2818Apply Operations to Maximize ScoreHardStack, Greedy, Array +4
2862Maximum Element-Sum of a Complete Subset of IndicesHardArray, Math, Number Theory
2867Count Valid Paths in a TreeHardTree, Depth-First Search, Math +2
2183Count Array Pairs Divisible by KHardArray, Math, Number Theory
2941Maximum GCD-Sum of a SubarrayPremiumHardArray, Math, Binary Search +1
2949Count Beautiful Substrings IIHardHash Table, Math, String +2

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.