diff --git a/FindDisappearedNumbers.java b/FindDisappearedNumbers.java new file mode 100644 index 00000000..02a0d41c --- /dev/null +++ b/FindDisappearedNumbers.java @@ -0,0 +1,32 @@ +import java.util.*; +class Solution { + + // TC - O(n) + // SC - O(1) + + /* + We are updating the input array itself to not use any extra space. + */ + public List findDisappearedNumbers(int[] nums) { + // iterate array, get value at index and subtract 1 (nums[i] - 1) to get the index at which the number should be present (if it was sorted) and multiply num by -1 + for (int i = 0; i < nums.length; i++) { + // take absolute value of the element (in case we mofified it with multiplying by -1) + int index = Math.abs(nums[i]) - 1; + // if valu at index is greater than 0, then multiply by -1 (this marks that the number index + 1 exists) + if (nums[index] > 0) { + nums[index] *= -1; + } + } + + List list = new ArrayList<>(); + + // iterate the array and if number at index is not -ve, it means that the number (index + 1) does exist in the array + for (int i = 0; i < nums.length; i++) { + if (nums[i] > 0) { + list.add(i + 1); + } + } + + return list; + } +} \ No newline at end of file diff --git a/GameOfLife.java b/GameOfLife.java new file mode 100644 index 00000000..9d65e938 --- /dev/null +++ b/GameOfLife.java @@ -0,0 +1,64 @@ +class Solution { + // TC - O(n) + // SC - O(1) + public void gameOfLife(int[][] board) { + int m = board.length; + int n = board[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + // get the neighbouring count of live cells + int count = countLiveCells(board, i, j, m, n); + + // if element at index is 1 but count of live cells < 2 or >=4 + // then set the element value to 2 (we choose 2 so that we can modify the same input array + // to find the next state of matrix) + + // so if value is being updated from 0 -> 1 we set it to -1 + // if value is updated from 1 -> 0 we set it as 2 + if (board[i][j] == 1) { + // set value as 2 if 1 -> 0 + if (count < 2 || count >= 4) { + board[i][j] = 2; + } + } + // if element at index is 0 but count of live cells is 3 + // then set the element value to -1 + if (board[i][j] == 0 && count == 3) { + // set value as -1 if 0 -> 1 + board[i][j] = -1; + } + } + } + + // to reset the input array value to the original value + // if value is 2 -> set it as 0, if value is -1 -> set it as 1 + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + if (board[i][j] == -1) { + board[i][j] = 1; + } + if (board[i][j] == 2) { + board[i][j] = 0; + } + } + } + } + + private int countLiveCells(int[][] board, int i, int j, int m, int n) { + int count = 0; + // we take an array of directions that we need to add to indexes to find their neighbouring cells + int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; + + for (int[] dir : dirs) { + int nr = i + dir[0]; + int nc = j + dir[1]; + + // if index is not out of bounds of the matrix and value is > 0 (1 or 2), then incr the count of live cells + if (nc >= 0 && nc < n && nr >= 0 && nr < m && board[nr][nc] > 0) { + count++; + } + } + + return count; + } +} \ No newline at end of file diff --git a/MinAndMax.java b/MinAndMax.java new file mode 100644 index 00000000..f388337c --- /dev/null +++ b/MinAndMax.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; +import java.util.Arrays; + +public class MinAndMax { + // TC = O(3n/2) + // SC = O(1) + public ArrayList findMinMax(int[] arr) { + int min, max, i; + + // to handle array with odd length, initialize min and max with arr[0] + // and iterate rest of the array in the same way + if (arr.length % 2 == 1) { + min = max = arr[0]; + i = 1; + } else { + // else set min and max from the first two indexes + if (arr[0] < arr[1]) { + min = arr[0]; + max = arr[1]; + } else { + min = arr[1]; + max = arr[0]; + } + i = 2; + } + + // We process elements in pairs to minimize the number of comparisons + while (i < arr.length - 1) { + //compare the two indexes to find the lower number + if (arr[i] < arr[i + 1]) { + min = Math.min(min, arr[i]); // lower number is used for min comparison + max = Math.max(max, arr[i + 1]); // higher value number is used to compare with max value + } else { + min = Math.min(min, arr[i + 1]); // lower number is used for min comparison + max = Math.max(max, arr[i]); // higher value number is used to compare with max value + } + i += 2; + } + + return new ArrayList(Arrays.asList(min, max)); + } +}