diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..9c6577a0 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,27 @@ +// Since 1 <= nums[i] <= n all the elements of the array are positive. so the idea is to temporarily +// multiply the element in the (nums[i]-1)th index with -1.This change of sign of (nums[i]-1)th index element +//indicates that element is present in array. Once the array iteration is done , the positve elements index+1 are the missing numbers +// Time Complexity: O(n) +// Space complexity: O(1) +class Problem1 { + public List findDisappearedNumbers(int[] nums) { + List result = new ArrayList<>(); + for(int i = 0; i0){ + nums[idx] *= -1; + } + } + for(int i = 0; i0){ + result.add(i+1); // +1 because ex: 4 is stored in index 3 + } + } + for(int i = 0; i getMinMax(int[] arr) { + ArrayList res = new ArrayList<>(); + int min = arr[0]; + int max = arr[0]; + int length = arr.length; + int remainder = length%2; + // find number of times we have to iterate based on length of array + // if length is even all iterations. If length is odd skip the ;ast iteration + // to avoid index out of bound + int iterations = remainder == 0 ? length : length-1; + + for(int i=0; i arr[i+1]) { + min = Math.min(min, arr[i+1]); + max = Math.max(max,arr[i]); + } else { + min = Math.min(min, arr[i]); + max = Math.max(max,arr[i+1]); + } + } + + // extra 2 comparisions for last element in odd length case. + if(remainder!=0){ + min = Math.min(min, arr[length-1]); + max = Math.max(max, arr[length-1]); + } + res.add(min); + res.add(max); + return res; + } +} diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..eecb4e39 --- /dev/null +++ b/Problem3.java @@ -0,0 +1,57 @@ +// Time Complexity: O(m*n) +// Space Complexity: O(1) +// We go cell by cell and count how many live neighbors it has. +// based on the given conditions we mark transitions with temporary values: 2 (alive → dead) and 3 (dead → alive). +// After iterating all the cells we convert 2 ->0 and 3 ->1 + +class Problem3 { + public void gameOfLife(int[][] board) { + int m = board.length; //rows + int n = board[0].length; // columns + + // alive to dead 1->0 : 2 + // dead to alive 0->1: 3 + for (int i = 0; i 3)) { + board[i][j] = 2; + } + // make it live cell + if(board[i][j] == 0 && liveCellCount == 3) { + board[i][j] = 3; + } + } + } + + // change back to 2 and 3 to actual dead or live cells + for (int i = 0; i0 + } + if(board[i][j] == 3) { + board[i][j] = 1; // it is changed from 0->1 + } + } + } + + } + + private int getLiveCellCount(int[][] board, int row, int column, int totalRows, int totalColumns){ + int count = 0; + // initialise the direction matrix for all 8 directions + int [][] dirs = new int[][]{{-1,0},{1,0},{0,1},{0,-1},{-1,1},{1,1},{-1,-1},{1,-1}}; + // iterate over direction array to get new adjacent cell live and find if new cell is live or not + for(int[] dir : dirs){ + int nr = row+dir[0]; + int nc = column+dir[1]; + // check for index out of bound and live cell + if(nr >=0 && nr < totalRows && nc >=0 && nc < totalColumns && (board[nr][nc] ==1 || board[nr][nc] == 2)){ + count++; + } + } + return count; + } +} \ No newline at end of file