Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Time Complexity : O(n) # We traverse array twice
# Space Complexity : O(1) # Ignoring the result array, we only use variables in-place
# Did this code successfully run on Leetcode : Yes
# Approach :
# 1. Use the values as indices: mark the index (num-1) as negative to say "this number exists".
# 2. After marking, indices that remain positive were never visited.
# 3. Those indices + 1 are the missing numbers.

from typing import List

class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
n = len(nums)

# First pass: mark numbers as seen by making their index negative
for i in range(n):
idx = abs(nums[i]) - 1 # map number to index (1 → 0, 2 → 1, ... n → n-1)
if nums[idx] > 0: # only flip if it's not already negative
nums[idx] *= -1 # mark index as visited

# Second pass: collect missing numbers
result = []
for i in range(n):
if nums[i] > 0: # if still positive → index never marked → number missing
result.append(i + 1) # convert back to 1-based number

return result
34 changes: 34 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Time Complexity : O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Approach :
# 1. Compare elements in pairs, reducing comparisons by half.
# 2. First compare the two elements to decide smaller/larger.
# 3. Update global min and max accordingly.

def getMinMax(arr):
n = len(arr)

# If array has even number of elements
if n % 2 == 0:
if arr[0] < arr[1]:
mn, mx = arr[0], arr[1]
else:
mn, mx = arr[1], arr[0]
i = 2
else:
# If odd, initialize with first element
mn = mx = arr[0]
i = 1

# Process pairs
while i < n - 1:
if arr[i] < arr[i+1]:
mn = min(mn, arr[i])
mx = max(mx, arr[i+1])
else:
mn = min(mn, arr[i+1])
mx = max(mx, arr[i])
i += 2

return mn, mx
60 changes: 60 additions & 0 deletions Problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Time Complexity : O(m * n)
# Space Complexity : O(1) # done in-place, no extra grid used
# Did this code successfully run on LeetCode : Yes
# Any problem you faced while coding this :
# Needed to carefully handle transitional states (1->0 and 0->1)


class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""

# All 8 possible directions around a cell
dirs = [(-1, -1), (-1, 0), (-1, 1),(0, -1),(0, 1),(1, -1), (1, 0), (1, 1)]

# m = number of rows, n = number of columns
m, n = len(board), len(board[0])


# Helper function to count live neighbors for cell (i, j)
def getCount(i, j):
count = 0
for dx, dy in dirs:
r, c = i + dx, j + dy
# Check if neighbor is within grid boundaries
if 0 <= r < m and 0 <= c < n:
# Count cells that are currently alive (1)
# or were alive but are marked to die (2)
if board[r][c] == 1 or board[r][c] == 2:
count += 1
return count

# First pass: determine next state for each cell
# Encode transitions without disturbing neighbors:
# 1 -> 0 becomes 2 (was live, will die)
# 0 -> 1 becomes 3 (was dead, will become live)

for i in range(m):
for j in range(n):
cnt = getCount(i, j)

# Rule 4: Dead cell with exactly 3 live neighbors -> becomes live
if board[i][j] == 0 and cnt == 3:
board[i][j] = 3
# Rule 1 & 3: Live cell with <2 or >3 live neighbors -> dies
elif board[i][j] == 1 and (cnt < 2 or cnt > 3):
board[i][j] = 2


# Second pass: finalize the board by decoding temporary states

for i in range(m):
for j in range(n):
# 3 means dead -> live, set to 1
if board[i][j] == 3:
board[i][j] = 1
# 2 means live -> dead, set to 0
elif board[i][j] == 2:
board[i][j] = 0