Math
Table of Contents
9. Palindrome Number
- LeetCode Link: Palindrome Number
- Difficulty: Easy
- Topic(s): Math, String Manipulation
- Company: Meta
🧠 Problem Statement
Given an integer
x, returntrueifxis a palindrome, andfalseotherwise.Example 1:
Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.Example 2:
Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.Example 3:
Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
🧩 Approach
- Convert the integer to a string.
- Compare the string with its reverse.
💡 Solution
class Solution:
def isPalindrome(self, x: int) -> bool:
"""
Check if an integer is a palindrome.
Args:
x (int): The integer to check.
Returns:
bool: True if x is a palindrome, False otherwise.
"""
if x < 0:
return False
s: str = str(x)
return s == s[::-1]
🧮 Complexity Analysis
- Time Complexity:
O(n) - Space Complexity:
O(n)
66. Plus One
- LeetCode Link: Plus One
- Difficulty: Easy
- Topic(s): Math, Array
- Company: Microsoft
🧠 Problem Statement
You are given a large integer represented as an integer array
digits, where eachdigits[i]is theithdigit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading0’s.Increment the large integer by one and return the resulting array of digits.
Example 1:
Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4].Example 2:
Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2].Example 3:
Input: digits = [9] Output: [1,0] Explanation: The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0].
🧩 Approach
- Reverse the digits array.
- Initialize a carry variable to 1 (to represent the increment).
- Iterate through the reversed digits:
- If the current digit is 9, set it to 0 (carry the 1).
- Otherwise, increment the current digit and set carry to 0.
- If there’s still a carry after the loop, append 1 to the result.
- Reverse the result back to the original order.
💡 Solution
from typing import List
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
"""
Increment the large integer represented as an array of digits by one.
Args:
digits (List[int]): The array of digits representing the integer.
Returns:
List[int]: The resulting array of digits after incrementing by one.
"""
digits = digits[::-1]
carry: int = 1
index: int = 0
while carry:
if index < len(digits):
if digits[index] == 9:
digits[index] = 0
else:
digits[index] += 1
carry = 0
else:
digits.append(1)
carry = 0
index += 1
return digits[::-1]
🧮 Complexity Analysis
- Time Complexity:
O(n) - Space Complexity:
O(1)
69. Sqrt(x)
- LeetCode Link: Sqrt(x)
- Difficulty: Easy
- Topic(s): Math, Binary Search
- Company: Amazon
🧠 Problem Statement
Given a non-negative integer
x, return the square root ofxrounded down to the nearest integer. The returned integer should be non-negative as well.You must not use any built-in exponent function or operator.
For example, do not use
pow(x, 0.5)in c++ orx ** 0.5in python.Example 1:
Input: x = 4 Output: 2 Explanation: The square root of 4 is 2, so we return 2.Example 2:
Input: x = 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
🧩 Approach
- Initialize two pointers:
Lset to 0 andRset tox. - While
Lis less than or equal toR:- Calculate the midpoint
Mas the average ofLandR. - If
M * Mis equal tox, returnM. - If
M * Mis less thanx, updateLtoM + 1. - If
M * Mis greater thanx, updateRtoM - 1.
- Calculate the midpoint
- If no exact square root is found, return
R.
💡 Solution
class Solution:
def mySqrt(self, x: int) -> int:
"""
Compute the integer square root of a non-negative integer x.
Args:
x (int): The non-negative integer.
Returns:
int: The integer square root of x.
"""
L: int = 0
R: int = x
while L <= R:
M = (L + R) // 2
M_squared = M * M
if M_squared == x:
return M
if M_squared < x:
L = M + 1
else:
R = M - 1
return R
🧮 Complexity Analysis
- Time Complexity:
O(log n) - Space Complexity:
O(1)
2169. Count Operations to Obtain Zero
- LeetCode Link: Count Operations to Obtain Zero
- Difficulty: Easy
- Topic(s): Math, Simulation, Weekly Contest 280
- Company: Capital One
🧠 Problem Statement
You are given two non-negative integers
num1andnum2.In one operation, if
num1 >= num2, you must subtractnum2fromnum1, otherwise subtractnum1fromnum2.
- For example, if
num1 = 5andnum2 = 4, subtractnum2fromnum1, thus obtainingnum1 = 1andnum2 = 4. However, ifnum1 = 4andnum2 = 5, after one operation,num1 = 4andnum2 = 1.Return the number of operations required to make either
num1 = 0ornum2 = 0.Example 1:
Input: num1 = 2, num2 = 3 Output: 3 Explanation: - Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1. - Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1. - Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1. Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations. So the total number of operations required is 3.Example 2:
Input: num1 = 10, num2 = 10 Output: 1 Explanation: - Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0. Now num1 = 0 and num2 = 10. Since num1 == 0, we are done. So the total number of operations required is 1.
🧩 Approach
- Initialize a result counter
resto 0. - While both
num1andnum2are greater than 0:- Add the integer division of
num1bynum2tores. - Update
num1to be the remainder ofnum1divided bynum2. - Swap
num1andnum2.
- Add the integer division of
- Return the result counter
res.
💡 Solution
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
"""
Count the number of operations to reduce either num1 or num2 to zero.
Args:
num1 (int): The first integer.
num2 (int): The second integer.
Returns:
int: The number of operations performed.
"""
res: int = 0
while num1 and num2:
res += num1 // num2
num1 = num1 % num2
num1, num2 = num2, num1
return res
🧮 Complexity Analysis
- Time Complexity:
O(log min(num1, num2)) - Space Complexity:
O(1)