From 1e9c895de7f15ac981cc62585cc88c2ff7b861b5 Mon Sep 17 00:00:00 2001 From: Nikesh Manjunath Date: Sat, 10 Jan 2026 17:16:30 -0800 Subject: [PATCH] Arrays-2 completed --- Leetcode289.java | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ Leetcode448.java | 28 ++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 Leetcode289.java create mode 100644 Leetcode448.java diff --git a/Leetcode289.java b/Leetcode289.java new file mode 100644 index 00000000..e1734123 --- /dev/null +++ b/Leetcode289.java @@ -0,0 +1,61 @@ +class Solution { + // This method will count all the ones in the neighbouring indices + private int countOnes(int[][] board, int row, int col) { + int[][] ids = new int[][] { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 }, + { 1, 1 } }; + int count = 0; + for (int[] id : ids) { + int r = row + id[0]; + int c = col + id[1]; + if (r >= 0 && r < board.length && c >= 0 && c < board[0].length) { + if (board[r][c] == 1 || board[r][c] == 2) { + count += 1; + } + } + } + + return count; + } + + public void gameOfLife(int[][] board) { + // 1 -> 2 -> 0 if < 2 1's + // 1 -> 1 -> 1 if =2,3 1's + // 1 -> 2 -> 0 if >3 1's + // 0 -> 3 -> 1 if =3 1's + int m = board.length; + int n = board[0].length; + + for (int r = 0; r < m; r++) { + for (int c = 0; c < n; c++) { + int count = countOnes(board, r, c); + if (board[r][c] == 0 && count == 3) { + board[r][c] = 3; // convert dead state to temp state (3) indicating it was in dead state earlier + // and needs to move to live state. + + } else if (board[r][c] == 1) { + if (count < 2 || count > 3) { + board[r][c] = 2; // convert live state to temp state (2) indicating it was in live state earlier + // and needs to move to dead state. + + } + } + + } + ; + } + + // At this point the board matrix will have the temp state so we'll convert 2->0 + // and 3->1 + 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; + } + + } + } + + } +} \ No newline at end of file diff --git a/Leetcode448.java b/Leetcode448.java new file mode 100644 index 00000000..619f6693 --- /dev/null +++ b/Leetcode448.java @@ -0,0 +1,28 @@ +// TC - O(n) +// SC - O(1) +class Solution { + public List findDisappearedNumbers(int[] nums) { + List res = new ArrayList<>(); + + for (int i = 0; i < nums.length; i++) { + int val = nums[i]; + if (val < 0) { + val = val * (-1); + } + if (nums[val - 1] > 0) { + nums[val - 1] = -1 * nums[val - 1]; + } + + } + + for (int i = 0; i < nums.length; i++) { + if (nums[i] > 0) { + res.add(i + 1); + } + + } + + return res; + + } +}