From 39ead80fcb847eb63334e670b878d76ecc640ff2 Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Wed, 14 Jan 2026 21:17:05 -0600 Subject: [PATCH] Array 2 done --- findDisappearedNumbers.js | 24 +++++++++++++++++ gameOfLife.js | 54 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 findDisappearedNumbers.js create mode 100644 gameOfLife.js diff --git a/findDisappearedNumbers.js b/findDisappearedNumbers.js new file mode 100644 index 00000000..15aefe54 --- /dev/null +++ b/findDisappearedNumbers.js @@ -0,0 +1,24 @@ +/** + * @param {number[]} nums + * @return {number[]} +Intuition: +Since the array contains numbers from 1 to n and has length n, every value corresponds to a valid index in the array (value − 1). +I am using the input array itself as a hash map by marking visited numbers as negative. For each number, we treat it's absolute value as a position and mark that index negative to indicate that this number exists. +After the first pass, any index that remains positive means its corresponding number (index + 1) was never seen in the array, so we add it to the result. This lets us find all missing numbers in O(n) time without extra space. + */ +var findDisappearedNumbers = function(nums) { + // first pass + for(let i = 0; i< nums.length; i++){ + const currNum = nums[i]; + const indexSupposedToBeIn = Math.abs(currNum)-1; + if(nums[indexSupposedToBeIn] > 0) nums[indexSupposedToBeIn] *= -1; + } + const result = []; + // second pass + for(let i = 0; i0){ + result.push(i+1); + } + } + return result; +}; \ No newline at end of file diff --git a/gameOfLife.js b/gameOfLife.js new file mode 100644 index 00000000..6d7b44c8 --- /dev/null +++ b/gameOfLife.js @@ -0,0 +1,54 @@ +/** + * @param {number[][]} board + * @return {void} Do not return anything, modify board in-place instead. +Inuition: + +The key idea is to update the board in-place without losing the original state needed to count neighbors. + +First scan each cell and determine its fate based on its live neighbors. Instead of changing 1 → 0 or 0 → 1 immediately, we temporarily mark transitions ('Y' for 1→0 and 'X' for 0→1).This allows us to still correctly count original live neighbors while processing the board. + +After the first pass, we do a second pass to finalize the state by converting 'Y'to 0 and'X'` to 1. This ensures the rules of the Game of Life are applied correctly using only constant extra space. + */ +var gameOfLife = function (board) { + const m = board.length; + const n = board[0].length; + // 8 directions + const directions = [ + [0, -1], [0, 1], [1, 0], [-1, 0], + [1, 1], [-1, -1], [1, -1], [-1, 1] + ]; + + const getNeighborCount = (row, col) => { + let count = 0; + for (let [rowAdd, colAdd] of directions) { + const newRow = row + rowAdd; + const newCol = col + colAdd; + if (newRow < 0 || newCol < 0 || newRow >= m || newCol >= n) continue; + if (board[newRow][newCol] === 1 || board[newRow][newCol] === 'Y') count++; + } + return count; + } + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + // get the live neighbors count + const count = getNeighborCount(i, j); + // if 0 turns to 1, mark it as X else Y + if (board[i][j] === 1) { + // dies by over and under population + if (count < 2 || count > 3) board[i][j] = 'Y'; // 1-> 0 + } else if (board[i][j] === 0) { + if (count === 3) board[i][j] = 'X'; // 0 -> 1 + } + + } + } + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (board[i][j] === 'Y') board[i][j] = 0; + else if (board[i][j] === 'X') board[i][j] = 1; + } + } + return board; +}; \ No newline at end of file