diff --git a/Problem-1.java b/Problem-1.java new file mode 100644 index 00000000..6b2d2720 --- /dev/null +++ b/Problem-1.java @@ -0,0 +1,32 @@ +// Time Complexity : O(n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this :no + + +// Your code here along with comments explaining your approach +// Use the input array itself to track which numbers have been seen by marking their corresponding indices as negative. +// After marking, any index that remains positive indicates that its number was never visited. +// Collect all such indices + 1 to get the list of missing numbers. + +class Solution { + public List findDisappearedNumbers(int[] nums) { + // Mark each number's corresponding index as negative + for (int i = 0; i < nums.length; i++) { + int idx = Math.abs(nums[i]) - 1; + if (nums[idx] > 0) { + nums[idx] = -nums[idx]; + } + } + + // Collect all indices that are still positive → missing numbers + List output = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) { + if (nums[i] > 0) { + output.add(i + 1); + } + } + + return output; + } +} \ No newline at end of file diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..3d48b237 --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,22 @@ +# Time Complexity : O(n) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode :yes +# Any problem you faced while coding this :no + + +# Your code here along with comments explaining your approach +# Use the input array itself to track which numbers have been seen by marking their corresponding indices as negative. +# After marking, any index that remains positive indicates that its number was never visited. +# Collect all such indices + 1 to get the list of missing numbers. + +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + output = [] + for i in range(len(nums)): + idx = abs(nums[i])-1 + if nums[idx]>0: + nums[idx] = -1*nums[idx] + for i in range(len(nums)): + if nums[i]>0: + output.append(i+1) + return output \ No newline at end of file diff --git a/Problem-2.java b/Problem-2.java new file mode 100644 index 00000000..746c5f3e --- /dev/null +++ b/Problem-2.java @@ -0,0 +1,58 @@ +// Time Complexity :O(n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :no +// Any problem you faced while coding this :no + + +// Your code here along with comments explaining your approach +// Initialize the global min and max by comparing the first one or two elements. +// Scan the array in pairs, comparing each pair to find a local min and max. +// Update the global min and max using each pair’s local results to reduce total comparisons. +class Problem2 { + + public int[] findMinAndMax(int[] nums) { + if (nums == null || nums.length == 0) { + return new int[]{}; + } + + int min, max; + int start; + + // Initialize min and max + if (nums.length % 2 == 0) { + // Even length → compare first pair + if (nums[0] < nums[1]) { + min = nums[0]; + max = nums[1]; + } else { + min = nums[1]; + max = nums[0]; + } + start = 2; + } else { + // Odd length → start with first element + min = nums[0]; + max = nums[0]; + start = 1; + } + + // Process remaining elements in pairs + for (int i = start; i < nums.length; i += 2) { + int localMin, localMax; + + if (nums[i] < nums[i + 1]) { + localMin = nums[i]; + localMax = nums[i + 1]; + } else { + localMin = nums[i + 1]; + localMax = nums[i]; + } + + // Update global min and max + if (localMin < min) min = localMin; + if (localMax > max) max = localMax; + } + + return new int[]{min, max}; + } +} diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..60997df0 --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,43 @@ +# Time Complexity :O(n) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode :no +# Any problem you faced while coding this :no + + +# Your code here along with comments explaining your approach +# Initialize the global min and max by comparing the first one or two elements. +# Scan the array in pairs, comparing each pair to find a local min and max. +# Update the global min and max using each pair’s local results to reduce total comparisons. + +class Problem2: + + def findMinAndMax(self, nums): + if not nums: + return None, None + + # Initialize + if len(nums) % 2 == 0: + # Compare first pair + if nums[0] < nums[1]: + min_num, max_num = nums[0], nums[1] + else: + min_num, max_num = nums[1], nums[0] + start = 2 + else: + # Odd length, start with first element + min_num = max_num = nums[0] + start = 1 + + # Compare in pairs + for i in range(start, len(nums), 2): + if nums[i] < nums[i+1]: + local_min = nums[i] + local_max = nums[i+1] + else: + local_min = nums[i+1] + local_max = nums[i] + + min_num = min(min_num, local_min) + max_num = max(max_num, local_max) + + return min_num, max_num diff --git a/Problem-3.java b/Problem-3.java new file mode 100644 index 00000000..2364fbb7 --- /dev/null +++ b/Problem-3.java @@ -0,0 +1,60 @@ + +// Time Complexity : O(mn) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this :no + + +// Your code here along with comments explaining your approach +// Traverse the board and mark cells with intermediate states: `2` for live→dead, `3` for dead→live, based on the number of live neighbors. +// Count neighbors by considering cells that were originally alive (`1` or `2`) so that updates are done in-place without affecting the current state. +// In a second pass, finalize the board by converting intermediate states (`2→0`, `3→1`) to get the next state of the Game of Life. + +class Solution { + public void gameOfLife(int[][] board) { + if (board == null || board.length == 0 || board[0].length == 0) + return; + + int m = board.length; + int n = board[0].length; + int[][] dirs = {{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,1}, {1,-1}, {1,0}, {1,1}}; + + // First pass: mark intermediate states + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + int liveNeighbours = countNeighbours(board, i, j, m, n, dirs); + + if (board[i][j] == 1) { + if (liveNeighbours < 2 || liveNeighbours > 3) { + board[i][j] = 2; // live -> dead + } + } else { + if (liveNeighbours == 3) { + board[i][j] = 3; // dead -> live + } + } + } + } + + // Second pass: finalize the board + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (board[i][j] == 2) board[i][j] = 0; + else if (board[i][j] == 3) board[i][j] = 1; + } + } + } + + private int countNeighbours(int[][] board, int i, int j, int m, int n, int[][] dirs) { + int count = 0; + for (int[] dir : dirs) { + int r = i + dir[0]; + int c = j + dir[1]; + // Explicitly check for originally alive cells (1 or 2) + if (r >= 0 && r < m && c >= 0 && c < n && (board[r][c] == 1 || board[r][c] == 2)) { + count++; + } + } + return count; + } +} diff --git a/Problem-3.py b/Problem-3.py new file mode 100644 index 00000000..bc8b5ff5 --- /dev/null +++ b/Problem-3.py @@ -0,0 +1,50 @@ +# Time Complexity : O(mn) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode :yes +# Any problem you faced while coding this :no + + +# Your code here along with comments explaining your approach +# Traverse the board and mark cells with intermediate states: `2` for live→dead, `3` for dead→live, based on the number of live neighbors. +# Count neighbors by considering cells that were originally alive (`1` or `2`) so that updates are done in-place without affecting the current state. +# In a second pass, finalize the board by converting intermediate states (`2→0`, `3→1`) to get the next state of the Game of Life. + +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + """ + Do not return anything; modify board in-place. + """ + if not board or not board[0]: + return + + m, n = len(board), len(board[0]) + dirs = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)] + + # First pass: mark cells with intermediate states + for i in range(m): + for j in range(n): + live_neigh = self.countNeighbours(board, i, j, m, n, dirs) + + if board[i][j] == 1: + if live_neigh < 2 or live_neigh > 3: + board[i][j] = 2 # live -> dead + else: + if live_neigh == 3: + board[i][j] = 3 # dead -> live + + # Second pass: finalize the board + for i in range(m): + for j in range(n): + if board[i][j] == 2: + board[i][j] = 0 + elif board[i][j] == 3: + board[i][j] = 1 + + def countNeighbours(self, board: List[List[int]], i: int, j: int, m: int, n: int, dirs: List[tuple]) -> int: + count = 0 + for dx, dy in dirs: + r, c = i + dx, j + dy + # Explicitly check for original alive cells (1 or 2) + if 0 <= r < m and 0 <= c < n and (board[r][c] == 1 or board[r][c] == 2): + count += 1 + return count