Number Theory LeetCode Problems: All 54,
With Python Solutions

Every problem in this library that LeetCode tags Number Theory. 47 of them carry a complete Python solution, a worked example and the time and space complexity of the approach.

Easy
8
Medium
24
Hard
22
1 pattern

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.

Eyes only

When the Number Theory problem arrives live.

In the live round, Stealth Interview reads the 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 says. It's a desktop app for macOS and Windows, invisible to screen sharing.

Number Theory problems by difficulty

Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (8)

Easy problems
No.Problem
0258Add Digits
0914X of a Kind in a Deck of Cards
1952Three Divisors
1979Find Greatest Common Divisor of Array
2413Smallest Even Multiple
2427Number of Common Factors
2614Prime In Diagonal
2748Number of Beautiful Pairs

Medium (24)

Medium problems
No.Problem
0204Count Primes
0858Mirror Reflection
0866Prime Palindrome
1201Ugly Number III
1447Simplified Fractions
1492The kth Factor of n
2001Number of Pairs of Interchangeable Rectangles
2280Minimum Lines to Represent a Line Chart
2447Number of Subarrays With GCD Equal to K
2470Number of Subarrays With LCM Equal to K
2507Smallest Value After Replacing With Sum of Prime Factors
2513Minimize the Maximum of Two Arrays
2521Distinct Prime Factors of Product of Array
2523Closest Prime Numbers in Range
2601Prime Subtraction Operation
2607Make K-Subarray Sums Equal
2654Minimum Number of Operations to Make All Array Elements Equal to 1
2761Prime Pairs With Target Sum
2807Insert Greatest Common Divisors in Linked List
2947Count Beautiful Substrings I
2436Minimum Split Into Subarrays With GCD Greater Than OnePremium
2464Minimum Subarrays in a Valid SplitPremium
2979Most Expensive Item That Can Not Be BoughtPremium
2992Number of Self-Divisible PermutationsPremium

Hard (22)

Hard problems
No.Problem
0952Largest Component Size by Common Factor
1250Check If It Is a Good Array
1627Graph Connectivity With Threshold
1735Count Ways to Make Array With Product
1766Tree of Coprimes
1799Maximize Score After N Operations
1808Maximize Number of Nice Divisors
1819Number of Different Subsequences GCDs
1994The Number of Good Subsets
1998GCD Sort of an Array
2197Replace Non-Coprime Numbers in Array
2338Count the Number of Ideal Arrays
2344Minimum Deletions to Make Array Divisible
2543Check if Point Is Reachable
2584Split the Array to Make Coprime Products
2709Greatest Common Divisor Traversal
2818Apply Operations to Maximize Score
2862Maximum Element-Sum of a Complete Subset of Indices
2867Count Valid Paths in a Tree
2183Count Array Pairs Divisible by K
2941Maximum GCD-Sum of a SubarrayPremium
2949Count Beautiful Substrings II