LeetCodechevron_rightCategorieschevron_rightnumber theory
label

number theory

73 problems
Easy: 11Medium: 30Hard: 32

number theory is one of the most repeated interview dimensions. Start with edge-safe fundamentals, then move into pattern-level trade-offs.

Interview Signal

Frequently tests problem modeling, edge handling, and verbal clarity.

Common Pitfall

Template-only answers break under follow-up questioning.

Practice Strategy

Practice in 3-5 problem rounds and always review complexity alternatives.

Recommended Progression

#TitleDifficulty
204

Count Primes

Count all prime numbers less than a given integer n using efficient array and math-based enumeration techniques.

Medium
258

Add Digits

Add Digits involves repeatedly summing digits of a number until a single digit is obtained.

Easy
858

Mirror Reflection

Given a square room with mirrors, find which receptor a laser ray will hit first based on two integers, p and q.

Medium
866

Prime Palindrome

The Prime Palindrome problem asks for the smallest prime palindrome greater than or equal to a given integer.

Medium
914

X of a Kind in a Deck of Cards

Solve the problem of determining if a deck can be partitioned into groups with equal occurrences of card values.

Easy
952

Largest Component Size by Common Factor

Find the largest connected component in an array where edges exist between numbers sharing a common factor greater than …

Hard
1201

Ugly Number III

Find the nth positive integer divisible by a, b, or c using binary search over the answer space efficiently and accurate…

Medium
1250

Check If It Is a Good Array

Determine if a given array of positive integers can generate 1 using integer multiples of any subset, leveraging number …

Hard
1447

Simplified Fractions

Generate simplified fractions between 0 and 1 with denominators up to a given integer n.

Medium
1492

The kth Factor of n

Find the kth factor of n by scanning divisors carefully or using factor pairs to skip unnecessary checks.

Medium
1627

Graph Connectivity With Threshold

In 'Graph Connectivity With Threshold,' determine if cities are connected based on common divisors exceeding a threshold…

Hard
1735

Count Ways to Make Array With Product

Determine the number of arrays of size n where the product equals k using prime factorization and combinatorial DP techn…

Hard
1766

Tree of Coprimes

Determine the closest coprime ancestor for each node in a tree using efficient traversal and state tracking of node valu…

Hard
1799

Maximize Score After N Operations

Maximize the score after n operations by selecting pairs from the array and using their GCD with dynamic programming or …

Hard
1808

Maximize Number of Nice Divisors

Solve Maximize Number of Nice Divisors by splitting primeFactors into mostly 3s and using fast modular exponentiation.

Hard
1819

Number of Different Subsequences GCDs

Given an array of positive integers, find the number of different subsequences' GCDs.

Hard
1952

Three Divisors

Determine if a given integer has exactly three positive divisors.

Easy
1979

Find Greatest Common Divisor of Array

Find the greatest common divisor of the smallest and largest numbers in an integer array.

Easy
1994

The Number of Good Subsets

Find the number of good subsets in an integer array, where each subset's product is the product of distinct primes.

Hard
1998

GCD Sort of an Array

The GCD Sort problem challenges you to sort an array using a specific gcd-based swap method.

Hard
2001

Number of Pairs of Interchangeable Rectangles

Count all pairs of rectangles that share the exact width-to-height ratio using efficient array scanning and hash lookup.

Medium
2183

Count Array Pairs Divisible by K

Count Array Pairs Divisible by K requires counting index pairs whose products are divisible by a given number k.

Hard
2197

Replace Non-Coprime Numbers in Array

Replace Non-Coprime Numbers in Array uses stack-based state management to iteratively merge adjacent non-coprime integer…

Hard
2280

Minimum Lines to Represent a Line Chart

Determine the fewest lines needed to accurately connect stock price points in a line chart using array and math reasonin…

Medium
2338

Count the Number of Ideal Arrays

This problem involves counting the number of ideal arrays of a given length under certain conditions using state transit…

Hard
2344

Minimum Deletions to Make Array Divisible

Find the minimum number of deletions to make the smallest element in nums divide all elements of numsDivide.

Hard
2413

Smallest Even Multiple

Find the smallest even multiple of a given integer using math and number theory concepts efficiently.

Easy
2427

Number of Common Factors

The problem asks to find the number of common factors of two positive integers a and b.

Easy
2447

Number of Subarrays With GCD Equal to K

Count the number of subarrays with GCD equal to a given value k from a list of integers.

Medium
2470

Number of Subarrays With LCM Equal to K

Find the number of subarrays in an array where the least common multiple (LCM) of the subarray equals a given integer k.

Medium
2507

Smallest Value After Replacing With Sum of Prime Factors

Replace a number with the sum of its prime factors until it stabilizes, and return the smallest value.

Medium
2513

Minimize the Maximum of Two Arrays

Minimize the Maximum of Two Arrays requires finding the smallest possible maximum number across two arrays, using binary…

Medium
2521

Distinct Prime Factors of Product of Array

Find the number of distinct prime factors in the product of an array of integers.

Medium
2523

Closest Prime Numbers in Range

Find the two closest prime numbers within a given range using efficient number theory techniques and gap comparisons.

Medium
2543

Check if Point Is Reachable

Determine if a target point is reachable from (1,1) using math-based moves following number theory rules efficiently.

Hard
2584

Split the Array to Make Coprime Products

Find the smallest index to split an array so that the products of two parts are coprime using array scanning and hash lo…

Hard
2601

Prime Subtraction Operation

Determine if it's possible to make the array strictly increasing using prime subtractions.

Medium
2607

Make K-Subarray Sums Equal

Solve Make K-Subarray Sums Equal by grouping circular indices with gcd cycles and minimizing each group to its median.

Medium
2614

Prime In Diagonal

Find the largest prime number located on any diagonal of a square matrix using array iteration and prime checking techni…

Easy
2654

Minimum Number of Operations to Make All Array Elements Equal to 1

Find the minimum number of operations to transform every element of a positive integer array into 1 using gcd operations…

Medium
2709

Greatest Common Divisor Traversal

Determine if every index in an array can be reached from any other using traversals based on greatest common divisors.

Hard
2748

Number of Beautiful Pairs

Count all index pairs in an array where the first digit of one number and last digit of another are coprime efficiently.

Easy
2761

Prime Pairs With Target Sum

Find all prime number pairs that sum up to a given integer n using an efficient sieve-based array approach.

Medium
2807

Insert Greatest Common Divisors in Linked List

The problem involves inserting greatest common divisors between adjacent nodes in a linked list.

Medium
2818

Apply Operations to Maximize Score

Maximize the score by applying operations on a subarray at most k times, utilizing stack-based state management.

Hard
2862

Maximum Element-Sum of a Complete Subset of Indices

Given a 1-indexed array, select a subset where indices' product is a perfect square, then return the maximum sum.

Hard
2867

Count Valid Paths in a Tree

Count Valid Paths in a Tree involves finding paths with exactly one prime number in a tree of n nodes.

Hard
2947

Count Beautiful Substrings I

Given a string and a value k, count the number of beautiful substrings where vowels * consonants % k == 0.

Medium
2949

Count Beautiful Substrings II

Count Beautiful Substrings II focuses on finding beautiful substrings with hash tables and number theory techniques.

Hard
3012

Minimize Length of Array Using Operations

Minimize the length of an integer array through a series of operations, using a greedy approach with modular arithmetic.

Medium
3044

Most Frequent Prime

Find the most frequent prime over 10 from numbers generated by scanning a 2D matrix in all straight directions efficient…

Medium
3115

Maximum Prime Difference

Calculate the largest index gap between prime numbers in an array using array traversal and number theory insights effic…

Medium
3116

Kth Smallest Amount With Single Denomination Combination

Find the kth smallest amount using only one coin denomination at a time, applying binary search efficiently over possibl…

Hard
3233

Find the Count of Numbers Which Are Not Special

Count the numbers between two integers that are not special, where special numbers are squares of primes.

Medium
3260

Find the Largest Palindrome Divisible by K

Compute the largest n-digit integer divisible by k that forms a palindrome using state transition dynamic programming te…

Hard
3312

Sorted GCD Pair Queries

Solve the Sorted GCD Pair Queries problem by efficiently counting and locating GCDs of all array pairs using hash mappin…

Hard
3326

Minimum Division Operations to Make Array Non Decreasing

Determine the minimum number of division operations to make an array non-decreasing using a greedy and invariant-based s…

Medium
3334

Find the Maximum Factor Score of Array

Calculate the maximum factor score of an integer array by optionally removing one element using LCM and GCD computations…

Medium
3336

Find the Number of Subsequences With Equal GCD

Count all pairs of non-empty subsequences in an integer array whose elements share the same greatest common divisor effi…

Hard
3348

Smallest Divisible Digit Product II

Find the smallest zero-free number at least as large as num whose digits multiply to a product divisible by t using care…

Hard
3377

Digit Operations to Make Two Integers Equal

Transform n into m using allowed digit operations without creating primes, applying math and graph strategies efficientl…

Medium
3378

Count Connected Components in LCM Graph

Determine the number of connected components in an LCM-based graph using array scanning and hash-based connectivity chec…

Hard
3411

Maximum Subarray With Equal Products

This problem involves finding the longest subarray where the product equals the LCM multiplied by the GCD, leveraging a …

Easy
3444

Minimum Increments for Target Multiples in an Array

This problem involves incrementing elements of an array to make sure each target element has at least one multiple in th…

Hard
3461

Check If Digits Are Equal in String After Operations I

Simulate repeated adjacent digit sums modulo 10 until two digits remain, then check whether those final digits match.

Easy
3463

Check If Digits Are Equal in String After Operations II

Determine if repeated digit-sum operations on a numeric string reduce it to two equal digits using math and string techn…

Hard
3556

Sum of Largest Prime Substrings

Compute the sum of the three largest unique primes from all substrings using hash table plus math efficiently.

Medium
3569

Maximize Count of Distinct Primes After Split

Compute the maximum number of distinct prime numbers after sequentially updating array elements with efficient preproces…

Hard
3574

Maximize Subarray GCD Score

Maximize Subarray GCD Score focuses on maximizing a subarray's score by using at most k doubling operations on an array …

Hard
3589

Count Prime-Gap Balanced Subarrays

Count the number of prime-gap balanced subarrays in an integer array using sliding window techniques and running state u…

Medium
3591

Check if Any Element Has Prime Frequency

Check if any element in the array has a prime frequency count, leveraging array scanning and hash table lookup.

Easy
3605

Minimum Stability Factor of Array

The problem requires finding the minimum stability factor of an array by utilizing binary search and math-based optimiza…

Hard
3618

Split Array by Prime Indices

Split Array by Prime Indices challenges you to separate elements at prime positions and minimize the absolute sum differ…

Medium

Related Patterns

Number Theory LeetCode Problems: 73 Solutions