diff --git a/Problem31.java b/Problem31.java new file mode 100644 index 00000000..a7e9f1eb --- /dev/null +++ b/Problem31.java @@ -0,0 +1,29 @@ +// 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 +class Solution { + public List findDisappearedNumbers(int[] nums) { + int n = nums.length; + List result = new ArrayList<>(); + + for (int i = 0; i < n; i++) { + int idx = Math.abs(nums[i]) - 1; + + if (nums[idx] > 0) { + nums[idx] *= -1; + } + } + + for (int i = 0; i < n; i++) { + if (nums[i] > 0) { + result.add(i+1); + } + } + + return result; + } +} \ No newline at end of file diff --git a/Problem32.java b/Problem32.java new file mode 100644 index 00000000..fc4b46dd --- /dev/null +++ b/Problem32.java @@ -0,0 +1,44 @@ +// 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 +public class Solution { + + public int[] findMinAndMax(int[] nums) { + int n = nums.length; + int min, max; + int i; + + // Handle first pair (or first element if odd length) + if (n % 2 == 0) { + if (nums[0] < nums[1]) { + min = nums[0]; + max = nums[1]; + } else { + min = nums[1]; + max = nums[0]; + } + i = 2; + } else { + min = max = nums[0]; + i = 1; + } + + // Process pairs + while (i < n - 1) { + if (nums[i] < nums[i + 1]) { + min = Math.min(min, nums[i]); + max = Math.max(max, nums[i + 1]); + } else { + min = Math.min(min, nums[i + 1]); + max = Math.max(max, nums[i]); + } + i += 2; + } + + return new int[]{min, max}; + } +} \ No newline at end of file diff --git a/Problem33.java b/Problem33.java new file mode 100644 index 00000000..ac64eb4f --- /dev/null +++ b/Problem33.java @@ -0,0 +1,53 @@ +// 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 +class Solution { + int[][] dirs; + int m, n; + + public void gameOfLife(int[][] board) { + this.dirs = new int[][]{{-1,-1}, {-1,0}, {-1, 1}, {0, -1}, {0, 1}, {1,-1}, {1,0}, {1,1}}; + this.m = board.length; + this.n = board[0].length; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + int count = getCount(board, i, j); + if (board[i][j] == 0 && count == 3) { + board[i][j] = 3; + } else if (board[i][j] == 1 && (count < 2 || count > 3)) { + board[i][j] = 2; + } + } + } + + 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 getCount(int[][] board, int i, int j) { + int count = 0; + + for (int[] dir: dirs) { + int r = i + dir[0]; + int c = j + dir[1]; + + if (r >= 0 && c >= 0 && r < m && c < n) { + if (board[r][c] == 1 || board[r][c] == 2) count++; + } + } + + return count; + } +} \ No newline at end of file diff --git a/Sample.java b/Sample.java deleted file mode 100644 index f5c45b5f..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach \ No newline at end of file