From cc86d1a935bf4998714830f996f3b84c5f122a0d Mon Sep 17 00:00:00 2001 From: rvuyyuru7 Date: Sat, 29 Nov 2025 19:48:50 -0600 Subject: [PATCH 1/2] Array-2 complete --- DisappearedNumbers.java | 27 +++++++++++++++++++++ GameOfLife.java | 54 +++++++++++++++++++++++++++++++++++++++++ MinMax.java | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 DisappearedNumbers.java create mode 100644 GameOfLife.java create mode 100644 MinMax.java diff --git a/DisappearedNumbers.java b/DisappearedNumbers.java new file mode 100644 index 00000000..df6cf958 --- /dev/null +++ b/DisappearedNumbers.java @@ -0,0 +1,27 @@ +import java.util.ArrayList; +import java.util.List; + +// Approach: Temporary state change pattern with two pass solution +// Time complexity: N + N ~= O(N) +// Space complexity: O(1) no extra space except for output. +class Solution { + public List findDisappearedNumbers(int[] nums) { + List result = new ArrayList<>(); + int length = nums.length; + for (int i = 0; i < length; i ++) { + int index = Math.abs(nums[i]) - 1; // Take absolute value to avoid double marking of the index + if (nums[index] > 0) { + nums[index] *= -1; // Mark the value at the index as negative + } + } + for (int i = 0; i < length; i ++) { + if (nums[i] > 0) { + // if the number is positive then i + 1 is missed/disappeared + result.add(i + 1); + } else { + nums[i] *= -1; // Changing the number to its original value + } + } + return result; + } +} \ No newline at end of file diff --git a/GameOfLife.java b/GameOfLife.java new file mode 100644 index 00000000..9bfe38d9 --- /dev/null +++ b/GameOfLife.java @@ -0,0 +1,54 @@ +// Approach: Temporary state change pattern to optimize space on previous solution +// Time: O(rows * columns) +// Space: O(1) +class Solution { + // Initialize directions + private static final int[][] directions = new int[][] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; + + public void gameOfLife(int[][] board) { + // Temporary states + // Change from 1 -> 0: Mark 2 (dies) + // Change from 0 -> 1: Mark 3 (lives) + int rows = board.length; + int columns = board[0].length; + for (int row = 0; row < rows; row ++) { + for (int column = 0; column < columns; column ++) { + int liveNeighbors = countLiveNeighbors(board, row, column); + if (board[row][column] == 1 && (liveNeighbors < 2 || liveNeighbors > 3)) { + board[row][column] = 2; // dies + } + if (board[row][column] == 0 && liveNeighbors == 3) { + board[row][column] = 3; // lives + } + } + } + // Putting back to actual values of live (1) or dead (0) + for (int row = 0; row < rows; row ++) { + for (int column = 0; column < columns; column ++) { + if (board[row][column] == 2) { // marked as dead + board[row][column] = 0; + } else if (board[row][column] == 3) { // marked as live + board[row][column] = 1; + } + } + } + } + + private int countLiveNeighbors(int[][] board, int row, int column) { + int countOfLiveNeighbors = 0; + // Iterate through all neighboring indices + for (int[] direction: directions) { + int neighborRow = row + direction[0]; + int neighborColumn = column + direction[1]; + if (neighborRow < 0 || neighborRow >= board.length || neighborColumn < 0 || neighborColumn >= board[0].length) { + // continue with next direction if current one is out of bounds + continue; + } + if (board[neighborRow][neighborColumn] == 1 || board[neighborRow][neighborColumn] == 2) { + // 1 represents live neighbors and 2 was alive originally + countOfLiveNeighbors ++; + } + } + return countOfLiveNeighbors; + } +} \ No newline at end of file diff --git a/MinMax.java b/MinMax.java new file mode 100644 index 00000000..c08cd6bb --- /dev/null +++ b/MinMax.java @@ -0,0 +1,45 @@ +import java.util.Arrays; + +// Approach: Minimizing number of comparisions to approximately 1.5 comparisions per index. +// Time complexity: N/2 ~= O(N) +// Space complexity: O(1) + +public class MinMax { + public int[] findMinMax(int[] nums) { + if (nums.length == 0) { + throw new IllegalArgumentException("No solution: Array is empty"); + } + int min = nums[0]; + int max = nums[0]; + int index = 1; + for (; index < nums.length; index += 2) { // increment 2 indices + if (nums[index - 1] < nums[index]) { + min = Math.min(min, nums[index - 1]); // compare min with smaller number + max = Math.max(max, nums[index]); // compare max with larger number + } else { + max = Math.max(max, nums[index - 1]); // compare max with larger number + min = Math.min(min, nums[index]); // compare min with smaller number + } + } + if (index == nums.length) { + // Handle last index + min = Math.min(min, nums.length - 1); + max = Math.max(max, nums.length - 1); + } + return new int[]{min, max}; + } + + public static void main(String[] args) { + MinMax ob = new MinMax(); + int[] nums1 = {3, 1, 0, 9, 4, 6}; + System.out.println(Arrays.toString(ob.findMinMax(nums1))); // returns [0, 9] + int[] nums2 = {3}; + System.out.println(Arrays.toString(ob.findMinMax(nums2))); // returns [0, 3] + int[] nums3 = {3, 1}; + System.out.println(Arrays.toString(ob.findMinMax(nums3))); // returns [1, 3] + int[] nums4 = {3, 1, 0, 9}; + System.out.println(Arrays.toString(ob.findMinMax(nums4))); // returns [0, 9] + int[] nums5 = {}; + System.out.println(Arrays.toString(ob.findMinMax(nums5))); // throws exception with message "No solution: Array is empty" + } +} From 883022807f26280c0542dd99472a8b3bd8492ce6 Mon Sep 17 00:00:00 2001 From: rvuyyuru7 Date: Sun, 30 Nov 2025 00:09:34 -0600 Subject: [PATCH 2/2] Fixed MinMax problem --- MinMax.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/MinMax.java b/MinMax.java index c08cd6bb..eb67ae45 100644 --- a/MinMax.java +++ b/MinMax.java @@ -23,8 +23,8 @@ public int[] findMinMax(int[] nums) { } if (index == nums.length) { // Handle last index - min = Math.min(min, nums.length - 1); - max = Math.max(max, nums.length - 1); + min = Math.min(min, nums[nums.length - 1]); + max = Math.max(max, nums[nums.length - 1]); } return new int[]{min, max}; } @@ -34,12 +34,20 @@ public static void main(String[] args) { int[] nums1 = {3, 1, 0, 9, 4, 6}; System.out.println(Arrays.toString(ob.findMinMax(nums1))); // returns [0, 9] int[] nums2 = {3}; - System.out.println(Arrays.toString(ob.findMinMax(nums2))); // returns [0, 3] + System.out.println(Arrays.toString(ob.findMinMax(nums2))); // returns [3, 3] int[] nums3 = {3, 1}; System.out.println(Arrays.toString(ob.findMinMax(nums3))); // returns [1, 3] int[] nums4 = {3, 1, 0, 9}; System.out.println(Arrays.toString(ob.findMinMax(nums4))); // returns [0, 9] - int[] nums5 = {}; - System.out.println(Arrays.toString(ob.findMinMax(nums5))); // throws exception with message "No solution: Array is empty" + int[] nums5 = {-1}; + System.out.println(Arrays.toString(ob.findMinMax(nums5))); // returns [-1, -1] + int[] nums6 = {3, -1}; + System.out.println(Arrays.toString(ob.findMinMax(nums6))); // returns [-1, 3] + int[] nums7 = {-1, -3, -2}; + System.out.println(Arrays.toString(ob.findMinMax(nums7))); // returns [-3, -1] + int[] nums8 = {-1, 7, -3, -2, 3}; + System.out.println(Arrays.toString(ob.findMinMax(nums8))); // returns [-3. 7] + int[] nums9 = {}; + System.out.println(Arrays.toString(ob.findMinMax(nums9))); // throws exception with message "No solution: Array is empty" } }