From 2f89c0f9d1464a7851e8f4824febeb5e1476f589 Mon Sep 17 00:00:00 2001 From: Ritish Date: Sun, 28 Sep 2025 21:15:12 -0700 Subject: [PATCH] Arrays-2 --- 31. Disappeared numbers in an Array.py | 25 ++++++++++++++ 32. Min and Max.py | 45 ++++++++++++++++++++++++++ 33. Game of Life.py | 43 ++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 31. Disappeared numbers in an Array.py create mode 100644 32. Min and Max.py create mode 100644 33. Game of Life.py diff --git a/31. Disappeared numbers in an Array.py b/31. Disappeared numbers in an Array.py new file mode 100644 index 00000000..3c5cc261 --- /dev/null +++ b/31. Disappeared numbers in an Array.py @@ -0,0 +1,25 @@ +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + # Iterating through every number in nums + for n in nums: + # Converting the number into its index (since numbers are 1..n, subtract 1) + i = abs(n) - 1 + + # Marking the number at index i as negative (if not already negative) + # This is showing that the number (i+1) exists in the array + nums[i] = -1 * abs(nums[i]) + + # Creating an empty list to collect missing numbers + result = [] + + # Iterating through nums again with index i and value n + for i, n in enumerate(nums): + # If a number is still positive, it means its index+1 was never seen + if n > 0: + result.append(i + 1) # Adding the missing number to result + + # Returning the final list of missing numbers + return result + +# Time Complexity: O(n) - We traverse the list a constant number of times. +# Space Complexity: O(1) - We use no extra space that scales with input size \ No newline at end of file diff --git a/32. Min and Max.py b/32. Min and Max.py new file mode 100644 index 00000000..db2f3dad --- /dev/null +++ b/32. Min and Max.py @@ -0,0 +1,45 @@ +class Solution: + # getMinMax is taking an array and returning a tuple (min, max) + # Iterating through the array and updating min and max accordingly + def getMinMax(self, arr: list) -> tuple: + n = len(arr) + # Handling edge case when array is empty + if n == 0: + return None, None + + # Initializing min and max + if n == 1: + # If only one element, returning it as both min and max + return arr[0], arr[0] + + if arr[0] > arr[1]: + min_val = arr[1] + max_val = arr[0] + else: + min_val = arr[0] + max_val = arr[1] + + # Iterating from the third element to the end + for i in range(2, n): + if arr[i] > max_val: + # Updating max if current element is greater + max_val = arr[i] + elif arr[i] < min_val: + # Updating min if current element is smaller + min_val = arr[i] + + # Returning min and max as a tuple + return min_val, max_val + +# Example usage +if __name__ == "__main__": + arr = [1000, 11, 445, 1, 330, 3000] + # Creating an instance of Solution + sol = Solution() + # Calling getMinMax and unpacking result + min_elem, max_elem = sol.getMinMax(arr) + print("Minimum element is", min_elem) + print("Maximum element is", max_elem) + +# Time Complexity (TC): O(n), where n is the number of elements in arr +# Space Complexity (SC): O(1), as only constant extra space is being used diff --git a/33. Game of Life.py b/33. Game of Life.py new file mode 100644 index 00000000..b6d7b350 --- /dev/null +++ b/33. Game of Life.py @@ -0,0 +1,43 @@ +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + # Getting the number of rows and columns in the board + rows, cols = len(board), len(board[0]) + + def countLiveNeighbors(r, c): + # Counting the number of live neighbors for cell (r, c) + live_neighbors = 0 + for i in range(r - 1, r + 2): + for j in range(c - 1, c + 2): + # Skipping the cell itself and out-of-bound indices + if (i == r and j == c) or i < 0 or j < 0 or i >= rows or j >= cols: + continue + # Checking if the neighbor is currently alive (1 or -1) + if abs(board[i][j]) == 1: + live_neighbors += 1 + return live_neighbors + + # Iterating through each cell to determine its next state + for r in range(rows): + for c in range(cols): + live_neighbors = countLiveNeighbors(r, c) + + # Marking live cells that should die as -1 + if board[r][c] == 1 and (live_neighbors < 2 or live_neighbors > 3): + board[r][c] = -1 + + # Marking dead cells that should become alive as 2 + if board[r][c] == 0 and live_neighbors == 3: + board[r][c] = 2 + + # Updating the board to the next state by converting temporary markers + for r in range(rows): + for c in range(cols): + # Setting cells to alive if their value is positive + if board[r][c] > 0: + board[r][c] = 1 + # Setting cells to dead otherwise + else: + board[r][c] = 0 + +# Time Complexity (TC): O(m * n), where m and n are the number of rows and columns, respectively. +# Space Complexity (SC): O(1), since the board is being updated in-place without using extra space. \ No newline at end of file