Math LeetCode Problems: All 485, With Python Solutions

Every problem in this library that LeetCode tags Math 485 in total, 413 of them with a complete Python solution, a worked example and the time and space complexity of the approach.

  • 485 problems
  • 121 Easy
  • 244 Medium
  • 120 Hard

How Math problems are solved

A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Math 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.

Math problems by difficulty

Showing the first 200 of 485 problems. Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (57)

#ProblemDifficultyTopics
9Palindrome NumberEasyMath
13Roman to IntegerEasyHash Table, Math, String
66Plus OneEasyArray, Math
67Add BinaryEasyBit Manipulation, Math, String +1
69Sqrt(x)EasyMath, Binary Search
70Climbing StairsEasyMemoization, Math, Dynamic Programming
168Excel Sheet Column TitleEasyMath, String
171Excel Sheet Column NumberEasyMath, String
202Happy NumberEasyHash Table, Math, Two Pointers
231Power of TwoEasyBit Manipulation, Recursion, Math
258Add DigitsEasyMath, Number Theory, Simulation
263Ugly NumberEasyMath
268Missing NumberEasyBit Manipulation, Array, Hash Table +3
292Nim GameEasyBrainteaser, Math, Game Theory
326Power of ThreeEasyRecursion, Math
342Power of FourEasyBit Manipulation, Recursion, Math
367Valid Perfect SquareEasyMath, Binary Search
405Convert a Number to HexadecimalEasyBit Manipulation, Math, String
412Fizz BuzzEasyMath, String, Simulation
415Add StringsEasyMath, String, Simulation
441Arranging CoinsEasyMath, Binary Search
492Construct the RectangleEasyMath
504Base 7EasyMath, String
507Perfect NumberEasyMath
509Fibonacci NumberEasyRecursion, Memoization, Math +1
598Range Addition IIEasyArray, Math
628Maximum Product of Three NumbersEasyArray, Math, Sorting
728Self Dividing NumbersEasyMath
762Prime Number of Set Bits in Binary RepresentationEasyBit Manipulation, Math
812Largest Triangle AreaEasyGeometry, Array, Math
836Rectangle OverlapEasyGeometry, Math
883Projection Area of 3D ShapesEasyGeometry, Array, Math +1
892Surface Area of 3D ShapesEasyGeometry, Array, Math +1
908Smallest Range IEasyArray, Math
914X of a Kind in a Deck of CardsEasyArray, Hash Table, Math +2
976Largest Perimeter TriangleEasyGreedy, Array, Math +1
989Add to Array-Form of IntegerEasyArray, Math
1025Divisor GameEasyBrainteaser, Math, Dynamic Programming +1
1030Matrix Cells in Distance OrderEasyGeometry, Array, Math +2
1037Valid BoomerangEasyGeometry, Array, Math
1071Greatest Common Divisor of StringsEasyMath, String
1103Distribute Candies to PeopleEasyMath, Simulation
1137N-th Tribonacci NumberEasyMemoization, Math, Dynamic Programming
1154Day of the YearEasyMath, String
1175Prime ArrangementsEasyMath
1185Day of the WeekEasyMath
1217Minimum Cost to Move Chips to The Same PositionEasyGreedy, Array, Math
1232Check If It Is a Straight LineEasyGeometry, Array, Math
1252Cells with Odd Values in a MatrixEasyArray, Math, Simulation
1266Minimum Time Visiting All PointsEasyGeometry, Array, Math
1281Subtract the Product and Sum of Digits of an IntegerEasyMath
1290Convert Binary Number in a Linked List to IntegerEasyLinked List, Math
1295Find Numbers with Even Number of DigitsEasyArray, Math
1304Find N Unique Integers Sum up to ZeroEasyArray, Math
1317Convert Integer to the Sum of Two No-Zero IntegersEasyMath
1323Maximum 69 NumberEasyGreedy, Math
1342Number of Steps to Reduce a Number to ZeroEasyBit Manipulation, Math

Medium (103)

#ProblemDifficultyTopics
2Add Two NumbersMediumRecursion, Linked List, Math
7Reverse IntegerMediumMath
12Integer to RomanMediumHash Table, Math, String
29Divide Two IntegersMediumBit Manipulation, Math
43Multiply StringsMediumMath, String, Simulation
48Rotate ImageMediumArray, Math, Matrix
50Pow(x, n)MediumRecursion, Math
62Unique PathsMediumMath, Dynamic Programming, Combinatorics
89Gray CodeMediumBit Manipulation, Math, Backtracking
96Unique Binary Search TreesMediumTree, Binary Search Tree, Math +2
150Evaluate Reverse Polish NotationMediumStack, Array, Math
166Fraction to Recurring DecimalMediumHash Table, Math, String
172Factorial Trailing ZeroesMediumMath
189Rotate ArrayMediumArray, Math, Two Pointers
204Count PrimesMediumArray, Math, Enumeration +1
223Rectangle AreaMediumGeometry, Math
227Basic Calculator IIMediumStack, Math, String
241Different Ways to Add ParenthesesMediumRecursion, Memoization, Math +2
264Ugly Number IIMediumHash Table, Math, Dynamic Programming +1
279Perfect SquaresMediumBreadth-First Search, Math, Dynamic Programming
313Super Ugly NumberMediumArray, Math, Dynamic Programming
319Bulb SwitcherMediumBrainteaser, Math
343Integer BreakMediumMath, Dynamic Programming
357Count Numbers with Unique DigitsMediumMath, Dynamic Programming, Backtracking
365Water and Jug ProblemMediumDepth-First Search, Breadth-First Search, Math
368Largest Divisible SubsetMediumArray, Math, Dynamic Programming +1
371Sum of Two IntegersMediumBit Manipulation, Math
372Super PowMediumMath, Divide and Conquer
375Guess Number Higher or Lower IIMediumMath, Dynamic Programming, Game Theory
380Insert Delete GetRandom O(1)MediumDesign, Array, Hash Table +2
382Linked List Random NodeMediumReservoir Sampling, Linked List, Math +1
384Shuffle an ArrayMediumDesign, Array, Math +1
390Elimination GameMediumRecursion, Math
396Rotate FunctionMediumArray, Math, Dynamic Programming
398Random Pick IndexMediumReservoir Sampling, Hash Table, Math +1
400Nth DigitMediumMath, Binary Search
423Reconstruct Original Digits from EnglishMediumHash Table, Math, String
445Add Two Numbers IIMediumStack, Linked List, Math
447Number of BoomerangsMediumArray, Hash Table, Math
453Minimum Moves to Equal Array ElementsMediumArray, Math
462Minimum Moves to Equal Array Elements IIMediumArray, Math, Sorting
464Can I WinMediumBit Manipulation, Memoization, Math +3
470Implement Rand10() Using Rand7()MediumMath, Rejection Sampling, Probability and Statistics +1
477Total Hamming DistanceMediumBit Manipulation, Array, Math
478Generate Random Point in a CircleMediumGeometry, Math, Rejection Sampling +1
486Predict the WinnerMediumRecursion, Array, Math +2
497Random Point in Non-overlapping RectanglesMediumReservoir Sampling, Array, Math +4
519Random Flip MatrixMediumReservoir Sampling, Hash Table, Math +1
523Continuous Subarray SumMediumArray, Hash Table, Math +1
528Random Pick with WeightMediumArray, Math, Binary Search +2
537Complex Number MultiplicationMediumMath, String, Simulation
539Minimum Time DifferenceMediumArray, Math, String +1
553Optimal DivisionMediumArray, Math, Dynamic Programming
556Next Greater Element IIIMediumMath, Two Pointers, String
592Fraction Addition and SubtractionMediumMath, String, Simulation
593Valid SquareMediumGeometry, Math
633Sum of Square NumbersMediumMath, Two Pointers, Binary Search
640Solve the EquationMediumMath, String, Simulation
6502 Keys KeyboardMediumMath, Dynamic Programming
667Beautiful Arrangement IIMediumArray, Math
670Maximum SwapMediumGreedy, Math
672Bulb Switcher IIMediumBit Manipulation, Depth-First Search, Breadth-First Search +1
738Monotone Increasing DigitsMediumGreedy, Math
754Reach a NumberMediumMath, Binary Search
775Global and Local InversionsMediumArray, Math
779K-th Symbol in GrammarMediumBit Manipulation, Recursion, Math
781Rabbits in ForestMediumGreedy, Array, Hash Table +1
788Rotated DigitsMediumMath, Dynamic Programming
789Escape The GhostsMediumArray, Math
808Soup ServingsMediumMath, Dynamic Programming, Probability and Statistics
837New 21 GameMediumMath, Dynamic Programming, Sliding Window +1
840Magic Squares In GridMediumArray, Hash Table, Math +1
858Mirror ReflectionMediumGeometry, Math, Number Theory
866Prime PalindromeMediumMath, Number Theory
869Reordered Power of 2MediumHash Table, Math, Counting +2
877Stone GameMediumArray, Math, Dynamic Programming +1
910Smallest Range IIMediumGreedy, Array, Math +1
932Beautiful ArrayMediumArray, Math, Divide and Conquer
939Minimum Area RectangleMediumGeometry, Array, Hash Table +2
963Minimum Area Rectangle IIMediumGeometry, Array, Hash Table +1
970Powerful IntegersMediumHash Table, Math, Enumeration
973K Closest Points to OriginMediumGeometry, Array, Math +4
991Broken CalculatorMediumGreedy, Math
1006Clumsy FactorialMediumStack, Math, Simulation
1015Smallest Integer Divisible by KMediumHash Table, Math
1017Convert to Base -2MediumMath
1033Moving Stones Until ConsecutiveMediumBrainteaser, Math
1040Moving Stones Until Consecutive IIMediumArray, Math, Sorting +1
1041Robot Bounded In CircleMediumMath, String, Simulation
1073Adding Two Negabinary NumbersMediumArray, Math
1093Statistics from a Large SampleMediumArray, Math, Probability and Statistics
1104Path In Zigzag Labelled Binary TreeMediumTree, Math, Binary Tree
1131Maximum of Absolute Value ExpressionMediumArray, Math
1140Stone Game IIMediumArray, Math, Dynamic Programming +2
1201Ugly Number IIIMediumMath, Binary Search, Combinatorics +1
1227Airplane Seat Assignment ProbabilityMediumBrainteaser, Math, Dynamic Programming +1
1237Find Positive Integer Solution for a Given EquationMediumMath, Two Pointers, Binary Search +1
1238Circular Permutation in Binary RepresentationMediumBit Manipulation, Math, Backtracking
1247Minimum Swaps to Make Strings EqualMediumGreedy, Math, String
1248Count Number of Nice SubarraysMediumArray, Hash Table, Math +2
1276Number of Burgers with No Waste of IngredientsMediumMath
1344Angle Between Hands of a ClockMediumMath
1352Product of the Last K NumbersMediumDesign, Array, Math +2

Hard (40)

#ProblemDifficultyTopics
60Permutation SequenceHardRecursion, Math
149Max Points on a LineHardGeometry, Array, Hash Table +1
224Basic CalculatorHardStack, Recursion, Math +1
233Number of Digit OneHardRecursion, Math, Dynamic Programming
273Integer to English WordsHardRecursion, Math, String
282Expression Add OperatorsHardMath, String, Backtracking
335Self CrossingHardGeometry, Array, Math
381Insert Delete GetRandom O(1) - Duplicates allowedHardDesign, Array, Hash Table +2
391Perfect RectangleHardGeometry, Array, Hash Table +2
458Poor PigsHardMath, Dynamic Programming, Combinatorics
479Largest Palindrome ProductHardMath, Enumeration
483Smallest Good BaseHardMath, Binary Search
564Find the Closest PalindromeHardMath, String
587Erect the FenceHardGeometry, Array, Math
668Kth Smallest Number in Multiplication TableHardMath, Binary Search
67924 GameHardArray, Math, Backtracking
710Random Pick with BlacklistHardArray, Hash Table, Math +3
780Reaching PointsHardMath
782Transform to ChessboardHardBit Manipulation, Array, Math +1
793Preimage Size of Factorial Zeroes FunctionHardMath, Binary Search
805Split Array With Same AverageHardBit Manipulation, Array, Math +2
810Chalkboard XOR GameHardBit Manipulation, Brainteaser, Array +2
829Consecutive Numbers SumHardMath, Enumeration
878Nth Magical NumberHardMath, Binary Search
887Super Egg DropHardMath, Binary Search, Dynamic Programming
891Sum of Subsequence WidthsHardArray, Math, Sorting
899Orderly QueueHardMath, String, Sorting
902Numbers At Most N Given Digit SetHardArray, Math, String +2
906Super PalindromesHardMath, String, Enumeration
913Cat and MouseHardGraph, Topological Sort, Memoization +3
920Number of Music PlaylistsHardMath, Dynamic Programming, Combinatorics
927Three Equal PartsHardArray, Math
952Largest Component Size by Common FactorHardUnion Find, Array, Hash Table +2
964Least Operators to Express NumberHardMemoization, Math, Dynamic Programming
996Number of Squareful ArraysHardBit Manipulation, Array, Hash Table +4
1012Numbers With Repeated DigitsHardMath, Dynamic Programming
1250Check If It Is a Good ArrayHardArray, Math, Number Theory
1307Verbal Arithmetic PuzzleHardArray, Math, String +1
1330Reverse Subarray To Maximize Array ValueHardGreedy, Array, Math
1359Count All Valid Pickup and Delivery OptionsHardMath, Dynamic Programming, Combinatorics

When the Math 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.