diff --git a/FindAllNumbersDisappearedinanArray.java b/FindAllNumbersDisappearedinanArray.java new file mode 100644 index 00000000..2d9d5bdf --- /dev/null +++ b/FindAllNumbersDisappearedinanArray.java @@ -0,0 +1,30 @@ +// 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 +/* +This problem can also be solved by maintaining a boolean array and marking that nums[i] - 1 as true and +return all those elements which remain false by traversing the boolean array again. But, the space complexity +will be O(n) for boolean array.So, to optimize, we traverse through input array and make the nums[i] - 1 +index's value as negative(if not). Finally, we return all those values which remain positive(means their +index not being called as that value is not present). We need to make sure of taking absolute value while +computing index as there is a chance of revisiting the indices during traversal. + */ +class Solution { + public List findDisappearedNumbers(int[] nums) { + List disappearedList = new ArrayList<>(); + int len = nums.length; + for(int i = 0 ; i < len ; i++) { + int index = Math.abs(nums[i]) - 1; + if(nums[index] > 0) + nums[index] *= -1; + } + for(int i = 0 ; i < len ; i++) { + if(nums[i] > 0) + disappearedList.add(i + 1); + } + return disappearedList; + } +} \ No newline at end of file diff --git a/GameofLife.java b/GameofLife.java new file mode 100644 index 00000000..68def865 --- /dev/null +++ b/GameofLife.java @@ -0,0 +1,54 @@ +// Time Complexity : O(m * 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 +/* +We create a function to basically check all neighbours at 8 directions(if applicable) and apply the given +problem's rules of live and dead cell count for each entry in the board.To make sure we are not counting +recently alive or ignoring recently dead cells, we mark them with 2 and 3 and later revert them in-place +to their actual states in the board. + */ +class Solution { + private int[][] dirs = new int[][]{{0, 1} , {1, 0} , {0, -1} , {-1, 0}, {-1, -1}, {-1, 1}, {1, 1}, {1, -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++) { + checkIfAlive(board, m , n, i , j); + } + } + // 1 -> 2 (previous alive, now dead) + // 0 -> 3 (previous dead, now alive) + 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 void checkIfAlive(int[][] board, int m, int n, int i , int j) { + int countLiveCell = 0; + for(int[] dir : dirs) { + int x = i + dir[0]; + int y = j + dir[1]; + if(x < 0 || y < 0 || x > m - 1 || y > n - 1) + continue; + if(board[x][y] == 1 || board[x][y] == 2) + countLiveCell++; + } + if(board[i][j] == 0 && countLiveCell == 3) + board[i][j] = 3; + else if(board[i][j] == 1 && (countLiveCell < 2 || countLiveCell > 3)) { + board[i][j] = 2; + } + } + +} \ No newline at end of file diff --git a/MaxandMinOfArray.java b/MaxandMinOfArray.java new file mode 100644 index 00000000..2ac81677 --- /dev/null +++ b/MaxandMinOfArray.java @@ -0,0 +1,52 @@ +// Time Complexity : O(n) with n comparisons +// Space Complexity : O(1) + +// Your code here along with comments explaining your approach +/* +Maintain min and max variables to track min and max values of array. now, compare consecutive elements to +find min and max between them and assign them to min and max values if applicable.This way, we can avoid +extra comparisons.This way, we can iterate every i+2 elements until end of array. To balance this check in +odd length arrays, we initialize the iterating i pointer accordingly. + */ +public class MaxandMinOfArray { + public static int[] findMaxAndMin(int[] nums) { + int n = nums.length; + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; + int i = 0; + + if(n % 2 == 0) { + if(nums[0] < nums[1]) { + min = Math.min(min, nums[0]); + max = Math.max(max, nums[1]); + } + else { + min = Math.min(min, nums[1]); + max = Math.max(max, nums[0]); + } + i = 2; + } + else { + min = max = nums[0]; + i = 1; + } + 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 = i + 2; + } + return new int[]{min, max}; + } + + public static void main(String[] args) { + int[] arr = new int[]{2, 3 , 1}; + int[] minMax = findMaxAndMin(arr); + System.out.println("Minimum element is " + minMax[0] + " and maximum element is " + minMax[1]); + } +} \ No newline at end of file