diff --git a/Find-all-numbers-disappeared-in-an-array.java b/Find-all-numbers-disappeared-in-an-array.java new file mode 100644 index 00000000..8409cd15 --- /dev/null +++ b/Find-all-numbers-disappeared-in-an-array.java @@ -0,0 +1,22 @@ +class Solution { + public List findDisappearedNumbers(int[] nums) { + int m = nums.length; + List list = new ArrayList<>(); + for(int j=0;j0){ + nums[x-1] *= -1; + } + + } + for(int j=0;j0){ + list.add(j+1); //add the indices + }else{ + nums[j] *= -1; //make the -ve elements as before to return as the original array + } + } + return list; + } +} \ No newline at end of file diff --git a/Game-of-life.java b/Game-of-life.java new file mode 100644 index 00000000..0c462ce8 --- /dev/null +++ b/Game-of-life.java @@ -0,0 +1,44 @@ +class Solution { + //1. 1-> <2 -> 0 ---2 + //2. 1-> 2or3 -> 1 + //3. 1-> >3 -> 0 --2 + //4. 0-> ==3 -> 1 ---3 + public void gameOfLife(int[][] board) { + int m = board.length; + int n = board[0].length; + int[][] dirs = new int[][]{{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,1},{1,-1}}; + for(int i=0;i 3)){ + board[i][j] = 2; + } + } + } + for(int i=0;i=0 && nc>=0 && nr nums[i+1]){ + min = Math.min(min, nums[i+1]); + max = Math.max(max, nums[i]); + }else{ + min = Math.min(min, nums[i]); + max = Math.max(max, nums[i+1]); + } + } + return new int[]{min, max}; + } +} \ No newline at end of file diff --git a/README.md b/README.md index 49598d54..ded6f5a4 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,19 @@ # Array-2 ## Problem1 (https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) - +* nums that are not there need to be return +* Bruteforce - boolean array. Whichever element is present, value is True, else it is False. But it is taking additional space. To avoid that use optimal approach. +* Optimal - Use Temporaray state pattern, whichever element is present, at what index that element would be present make it -ve. If it was already -ve take absolute value of that, then change to -ve. If its duplicate element, don't do anything, Let the -ve be -ve only. ## Problem2 Given an array of numbers of length N, find both the minimum and maximum. Follow up : Can you do it using less than 2 * (N - 2) comparison +* Bruteforce - check every element, it will have 2n comparisons. +* Optimal consider pair, from that pair choose min & max. Do it for N/2 pairs. it will have N-2 comparison for min & N-2 for max + ## Problem3 (https://leetcode.com/problems/game-of-life/) +* Bruteforce - we need to maintain extra apace for original state. +* Optimal - 1st & 4th condition where states are changing use Temporaray state pattern. Ex- 1-> <2 -> 0 ---2, 1-> >3 -> 0 --2, 0-> ==3 -> 1 ---3 +* func countLives - to count 1s in all 8 directions +