Day-1, Day-2 LeetCode Challenge-365-Days.

Aaqib Ali
3 min readJan 1, 2025

--

I have started my journey to prepare for Google interviews and set a complete year-long challenge. I will update here every problem I solve, sharing its logic and detailed Medium articles to describe my approach. Welcome to join me on this journey — a chance to learn different problem-solving techniques, share insights, and grow together. Feel free to reach out for discussions and collaboration via LinkedIn or email at aaqibalisahito44@gmail.com.

Day 1: Sum of Two Integers (LeetCode #371)

Logic: The task was to calculate the sum of two integers a and b without using the + or - operators. Instead, I used bitwise operators:

  1. XOR (^): Simulates addition without considering the carry.
  2. AND (&): Identifies the carry bits.
  3. Shift Left (<<): Moves the carry bits to their appropriate positions for addition.

By iterating until there is no carry left, the result accumulates in the variable.

Learnings:

  • XOR and AND are powerful tools for bitwise operations.
  • Handling negative numbers with two’s complement representation was insightful.
  • Optimized for O(n)O(n), where nn is the number of bits in the integers.

Steps:

  1. Initialize constants for 32-bit masking.
  2. Use XOR for sum and AND for carry.
  3. Shift the carry left.
  4. Return the result, accounting for negative numbers using two’s complement logic.

Python Implementation:

class Solution:
def getSum(self, a: int, b: int) -> int:
MASK = 0xFFFFFFFF
INT_MAX = 0x7FFFFFFF

while b != 0:
carry = (a & b) & MASK
a = (a ^ b) & MASK
b = (carry << 1) & MASK
return a if a <= INT_MAX else ~(a ^ MASK)
LeetCode Profile.

Day 2: Missing Number (LeetCode #268)

Logic: The problem required finding the missing number in a sequence ranging from 0 to n. XOR properties made the solution efficient:

  1. Initialize a result variable as the array’s size.
  2. XOR the result with each index and its corresponding array value.
  3. The unmatched number left after the XOR operations is the missing number.

Learnings:

  • XOR eliminates paired values, leaving the unpaired value as the result.
  • The approach leverages bitwise operations to reduce space complexity to O(1)O(1) while maintaining O(n)O(n) time complexity.

Steps:

  1. Initialize res with the array size.
  2. Iterate through the array, applying XOR between indices and values.
  3. Return the remaining value in res.

Python Implementation:

class Solution:
def missingNumber(self, nums):
res = len(nums)
for i in range(len(nums)):
res ^= nums[i]
res ^= i
return res

Second Approach Names

  • Sum of Two Integers: Alternate implementation using recursion.
  • Missing Number: Gauss’ formula to compute the expected sum and subtract the actual sum.

Thank you for reading! Your suggestions are welcome. Feel free to reach out for discussions or learning together. Let’s make this journey collaborative and insightful. You can contact me at LinkedIn or aaqibalisahito44@gmail.com.

--

--

No responses yet