From 2ce125579669182b8a7d054b6744ef6aca23d3a9 Mon Sep 17 00:00:00 2001 From: Manideep Pilli Date: Fri, 7 Nov 2025 17:54:44 -0700 Subject: [PATCH] Completed Array-2 --- ARR2_289_Game_of_Life.java | 64 ++++++++++++++ ...d_All_Numbers_Disappeared_in_an_Array.java | 87 +++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 ARR2_289_Game_of_Life.java create mode 100644 ARR2_448_Find_All_Numbers_Disappeared_in_an_Array.java diff --git a/ARR2_289_Game_of_Life.java b/ARR2_289_Game_of_Life.java new file mode 100644 index 00000000..eea63ab2 --- /dev/null +++ b/ARR2_289_Game_of_Life.java @@ -0,0 +1,64 @@ +/**** Method 1 ****/ +//Time Complexity: O(n^2) +//Space Complexity: O(1) + +//Successfully submitted in LeetCode + +//Traverse the arr, and at each index find the count of 1's if it is already 1 and count <2 or >3 then change it's value to 2 as we if alter it to "0" we may lose the state for others,else if is "0" and count is 3 we update the board value to 3. Later we convert all the 2 to 0 and 3 to 1 by traversing. + +public class ARR2_289_Game_of_Life { + + public void gameOfLife(int[][] board) { + //1 --> 0 : 2 + //0 --> 1 : 3 + + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[i].length; j++) { + int count = count1s(board, i, j); + if (board[i][j] == 1) { + if (count < 2 || count > 3) { + board[i][j] = 2; + } + } else { + if (count == 3) { + board[i][j] = 3; + } + } + } + } + + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[i].length; j++) { + if (board[i][j] == 2) board[i][j] = 0; + if (board[i][j] == 3) board[i][j] = 1; + } + } + } + + public int count1s(int[][] board, int i, int j) { + int count = 0; + + int mat[][] = { + { -1, -1 }, + { -1, 0 }, + { -1, 1 }, + { 0, -1 }, + { 0, 1 }, + { 1, -1 }, + { 1, 0 }, + { 1, 1 }, + }; + + for (int k = 0; k < mat.length; k++) { + int x = i + mat[k][0]; + int y = j + mat[k][1]; + if (x >= 0 && x < board.length && y >= 0 && y < board[i].length) { + if (board[x][y] == 1 || board[x][y] == 2) { + count++; + } + } + } + + return count; + } +} diff --git a/ARR2_448_Find_All_Numbers_Disappeared_in_an_Array.java b/ARR2_448_Find_All_Numbers_Disappeared_in_an_Array.java new file mode 100644 index 00000000..0e125893 --- /dev/null +++ b/ARR2_448_Find_All_Numbers_Disappeared_in_an_Array.java @@ -0,0 +1,87 @@ +/**** Method 1 ****/ +//Time Complexity: O(n^2) +//Space Complexity: O(1) + +//Successfully submitted in LeetCode + +//Take two loops one from 1 to n and another to traverse the arr and check if ele from first loop is present or not. If not add to missing list. At the end return missing list. + +/**** Method 2 ****/ +//Time Complexity: O(n) +//Space Complexity: O(n) + +//Successfully submitted in LeetCode + +//Traverse the arr and store all it's values in set, later start another loop from 1 to n and check if set contains it or not. If not add to missing list. At the end return missing list. + +/**** Method 3 ****/ +//Time Complexity: O(n) +//Space Complexity: O(1) + +//Successfully submitted in LeetCode + +//ONLY IF WE CAN MODIFY THE GIVEN ARR. As all the elements present should generally be less than n, we can traverse the arr and make the index of the value as -ve, so in this way all elements in arr will have there index marked -ve. So at end we can traverse the array and add the missing items(which values are not -ve) to list. + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +public class ARR2_448_Find_All_Numbers_Disappeared_in_an_Array { + + /**** Method 1 ****/ + public List findDisappearedNumbers1(int[] nums) { + List ans = new ArrayList<>(); + + for (int i = 1; i <= nums.length; i++) { + boolean flag = false; + int idx = 0; + while (idx < nums.length) { + if (nums[idx] == i) { + flag = true; + break; + } + } + if (!flag) { + ans.add(i); + } + } + return ans; + } + + /**** Method 2 ****/ + public List findDisappearedNumbers2(int[] nums) { + List ans = new ArrayList<>(); + HashSet set = new HashSet<>(); + + for (int i : nums) { + set.add(i); + } + + for (int i = 1; i <= nums.length; i++) { + if (!set.contains(i)) { + ans.add(i); + } + } + + return ans; + } + + /**** Method 3 ****/ + public List findDisappearedNumbers3(int[] nums) { + List ans = new ArrayList<>(); + + for (int i = 0; i < nums.length; i++) { + if (nums[Math.abs(nums[i]) - 1] > 0) { + nums[Math.abs(nums[i]) - 1] *= -1; + } + } + + for (int i = 0; i < nums.length; i++) { + if (nums[i] > 0) { + ans.add(i + 1); + } + } + + return ans; + } +}