From 8d5cd8e1f3f16d783f788cabf384577698e5cecc Mon Sep 17 00:00:00 2001 From: kalyan Date: Wed, 15 Oct 2025 17:26:08 -0500 Subject: [PATCH] Array-2 solutions --- FindAllNumbersMissingInArray.java | 26 +++++++++++++++ GameOfLife.java | 54 +++++++++++++++++++++++++++++++ MinAndMaxInArray.java | 33 +++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 FindAllNumbersMissingInArray.java create mode 100644 GameOfLife.java create mode 100644 MinAndMaxInArray.java diff --git a/FindAllNumbersMissingInArray.java b/FindAllNumbersMissingInArray.java new file mode 100644 index 00000000..2c779271 --- /dev/null +++ b/FindAllNumbersMissingInArray.java @@ -0,0 +1,26 @@ +// Time Complexity : O(n). +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Approach : If we sort the Array, as the condition is 1 to n numbers, the element should be at i+1 index. Assuming this logic, we +// iterate through array and make the index's value-1 index to be negative. Which means all the numbers already present will be negative. +// Now we iterate again, and the index+1 of the positive numbers ill give the result. + +class Solution { + public List findDisappearedNumbers(int[] nums) { + List res = new ArrayList<>(); + + for(int i=0;i 0){ + nums[val-1] = -nums[val-1]; //marking the value that is supposed to be in this position to negative + } + } + + for(int i=0;i 0){ //non-negative number's index will be the ones that are not visited. + res.add(i+1); + } + } + return res; + } +} \ No newline at end of file diff --git a/GameOfLife.java b/GameOfLife.java new file mode 100644 index 00000000..c1c5e611 --- /dev/null +++ b/GameOfLife.java @@ -0,0 +1,54 @@ +// Time Complexity : O(m*n). +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Approach : As per the problem and given conditions, we change the alive to dead ones to 2 and dead to alive ones to 3. As all +// operations happen simultaneously according to the problem, we shouldn't consider the updated values so we maintain 2 or 3 for updated +// ones. We first calculate the alives and perform the logic on the count, iterate the array again and transform 2 and 3 to alive or +// dead values in the matrix. + +class Solution { + int m, n; + + public void gameOfLife(int[][] board) { + this.m = board.length; + this.n = board[0].length; + for (int i=0;i 3)){ //as per the condition in problem + board[i][j] = 2; // alive to dead. + } + if(board[i][j] == 0 && alivesCount == 3){ + board[i][j] = 3;// dead to alive + } + } + + } + for(int i=0; i= 0 && row < m && col >= 0 && col < n) { // row and column are within borders. + if (board[row][col] == 1 || board[row][col] == 2) //as we changed to 2 for alive to dead. + count++; + } + } + return count; + } +} \ No newline at end of file diff --git a/MinAndMaxInArray.java b/MinAndMaxInArray.java new file mode 100644 index 00000000..5b35b215 --- /dev/null +++ b/MinAndMaxInArray.java @@ -0,0 +1,33 @@ +// Time Complexity : O(n). +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Approach : Comparison by pairs approach helps in reducing the comparisons that checking min and max at each value. We consider two +// elements in the array and based on the bigger value we compare with existing max and voce versa for min. If array size is odd I was +// missing the last element, so I have added an additional condition for it as well. + +class Solution { + public ArrayList getMinMax(int[] arr) { + // code Here + ArrayList res = new ArrayList<>(); + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; + int i=0; + for(i=0;i