diff --git a/Leetcode_289.java b/Leetcode_289.java new file mode 100644 index 00000000..bdfa7864 --- /dev/null +++ b/Leetcode_289.java @@ -0,0 +1,64 @@ +//In this problem, we need to update the given board. If we do so, we might loose old value. To retain it, we will update the values other than 0 or 1[as 0 and 1 are given in input] +//TC: 8*O(m*n); SC:O(1) + + +class Solution { + public void gameOfLife(int[][] board) { + + int n=board.length; + int m=board[0].length; + + for(int i=0;i3 || active<2)){ + board[i][j]=2; + } + + if(board[i][j] == 0 && (active==3)){ + board[i][j]=3; + } + + + } + } + + for(int i=0;i=0 && r=0 && c findDisappearedNumbers(int[] nums) { + Set set=new HashSet<>(); + + //first iteration + for(int i:nums){ + set.add(i); + } + + int n=nums.length; + + List ans=new ArrayList<>(); + //second iteration + for(int i=1;i<=n;i++){ + if(!(set.contains(i))){ + ans.add(i); + } + } + + return ans; + + } +} + +//way2 +//Mutate the original array by going to the exact position of element present at index +//nums[0]=5 -- go to index 4[because 5th element's position is at 4] and update it to negative value at 4th index. It means element 5 is present +//After updating the entire array with abive concept, travel array again to identify missing elements +//TC: O(2n); SC: constant +class Solution { + public List findDisappearedNumbers(int[] nums) { + int n=nums.length; + for(int i=0;i0){ + nums[idx]*=-1; + } + + } + + List ans=new ArrayList<>(); + + for(int i=0;i0){ + ans.add(i+1); + }else{ + nums[i]*=-1; + } + } + + return ans; + + } +}