From 7e5aa0fe25e65457218481db1a32a2a408c3e8ea Mon Sep 17 00:00:00 2001 From: Dainish-Chhaya Date: Sun, 16 Aug 2026 19:35:05 -0600 Subject: [PATCH] Array-2 Submission --- Problem1.java | 40 +++++++++++++++++++++++++++++ Problem2.java | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ Problem3.java | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 Problem1.java create mode 100644 Problem2.java create mode 100644 Problem3.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..d9cb0938 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,40 @@ +//Problem - LeetCode 448 Find All Numbers Disappeared in an Array +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Not able to come up with this logic on my own + +// Your code here along with comments explaining your approach +import java.util.*; + +//Approach - mark the numbers which are already there as negative. So in the second traversal we can iddentify the non negative numbers and their index + 1 should be the missing number + +public class Problem1 { + public List findDisappearedNumber(int [] nums){ + List result = new ArrayList<>(); + int n = nums.length; + + //first for the given number nums[i] -- see what should be its actual index + //for e.g. nums[i] = 4 --> its index should be 3; So mark nums[3] as negative (*-1) + + for(int i = 0; i < n; i++){ + int idx = Math.abs(nums[i]) - 1; + if(nums[idx] < 0) continue; //number already marked as negative + else nums[idx] *= -1; + } + + // In second traversal - whatever are the numbers are non negative, for them add their idx + 1 into the result + + for(int i = 0; i < n; i++){ + if(nums[i] < 0) nums[i] *= -1; // to keep original array as intact + else result.add(i+1); //number at that idx is non negative so idx + 1 would be missing so add it in the list. + } + return result; + } + + public static void main(String[] args) { + Problem1 demo = new Problem1(); + int [] nums = {4,3,2,7,8,2,3,1}; + System.out.println(demo.findDisappearedNumber(nums)); + } +} diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..8ab4c050 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,70 @@ +// Time Complexity :O (3N/2) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Problem not in leetcode +// Any problem you faced while coding this : Struggled to get the pairs logic for this problem + +//We will form pairs of two elements and compare the elements with each other. The greater number needs to be compared with max and the smaller number needs to be compared with min. So, with this approach we don't compare each element for max and min unnecessarily + + +// Your code here along with comments explaining your approach +public class Problem2 { + public void compareElements(int[]nums){ + int max = Integer.MIN_VALUE; + int min = Integer.MIN_VALUE; + int i; + + if(nums.length == 0 || nums == null) System.out.println("Array is empty"); + + if(nums.length == 1){ + max = nums[0]; + min = nums[0]; + } + + if(nums.length % 2 != 0){ + //array is odd. So consider first element as min and max both and start forming pairs + max = min = nums[0]; + i =1; // start with 1st element of array + } + + else{ + if (nums[0] < nums[1]){ + max = nums[1]; + min = nums[0]; + } + else { + max = nums[0]; + min = nums[1]; + } + i = 2; //start from index 2 + } + + while(i < nums.length -1){ + if(nums[i] < nums[i+1]){ + if(nums[i+1] > max) max = nums[i+1]; + if(nums[i] < min) min = nums[i]; + } + else{ + //nums[i] is greater + if(nums[i] > max) max = nums[i]; + if(nums[i+1] < min) min = nums[i+1]; + } + i += 2; + } + + System.out.println("Array size : " + nums.length); + System.out.println("Max : " + max); + System.out.println("Min : " + min); + } + public static void main(String[] args) { + Problem2 demo = new Problem2(); + + int [] nums = {3, 1, 4, 1, 5, 9}; + demo.compareElements(nums); + + int [] nums2 = {7, 2, 8, 3, 6}; + demo.compareElements(nums2); + + int [] nums3 = {1, 2, 3, 4, 5, 6}; + demo.compareElements(nums3); + } +} diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..2519842c --- /dev/null +++ b/Problem3.java @@ -0,0 +1,67 @@ +//Problem - 289. Game of Life +// Time Complexity : O(m*n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : Could not solve it on my own and faced syntax issues +public class Problem3 { + int[][] dirc; + int m , n; + + public void gameOfLife(int[][] board){ + this.m = board.length; + this.n = board[0].length; + //defining the directions + this.dirc = new int[][] {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,1},{1,0},{1,-1}}; + + //if prev dead now alive then change it with number - 11 + //if prev alive now dead then change it with number - 13 + + for(int i = 0; i < m; i++){ + for(int j = 0; j < n; j++){ + //this function will give us all the neighbouring 1s for each element. Pass the current element in it and it will give neighbouring 1s. Based on which it will be decided whether the current element will live or die. + + int count = getCount(board, i, j); + + if(board[i][j] == 0){ + if(count == 3) board[i][j] = 11; + } + else if(board[i][j] == 1){ + if(count <2 || count >3) board[i][j] = 13; + } + } + } + + //get the final array + for(int i = 0; i < m; i++){ + for(int j = 0; j < n; j++){ + if(board[i][j] == 11) board[i][j] = 1; + else if(board[i][j] == 13) board[i][j] = 0; + } + } + + + for(int i = 0; i < m; i++){ + for(int j = 0; j < n; j++){ + System.out.print(board[i][j]); + } + System.out.println(); + } + } + + private int getCount(int[][]board, int idx1, int idx2){ + int count = 0; + for(int[] dir : dirc){ + int r = idx1 +dir[0]; + int c = idx2 + dir[1]; + + if( r >= 0 && c >= 0 && r