From 7932afaa502d5098e50566df1d98cc7c59cf394e Mon Sep 17 00:00:00 2001 From: tmujje Date: Tue, 4 May 2021 22:11:18 -0400 Subject: [PATCH 1/2] Array-2 Assignment Submission --- DisappearedNumbers.java | 28 ++++++++++++++ FindMinMax.cs | 57 +++++++++++++++++++++++++++++ GameOfLife.java | 81 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 DisappearedNumbers.java create mode 100644 FindMinMax.cs create mode 100644 GameOfLife.java diff --git a/DisappearedNumbers.java b/DisappearedNumbers.java new file mode 100644 index 00000000..556356bc --- /dev/null +++ b/DisappearedNumbers.java @@ -0,0 +1,28 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n) since we are iterating through the array linearly +//Space Complexity: O(1) since we are not taking any extra space except result array + +public List findDisappearedNumbers(int[] nums) { + ArrayList returnList = new ArrayList<>(); + + if(nums == null || nums.length == 0) return returnList; + + // Iterate through the array and change the index corresponding to the element to a negative value + for(int i = 0; i < nums.length; i++){ + int index = Math.abs(nums[i]) - 1; + if(nums[index] > 0){ + nums[index] = -1 * nums[index]; + } + } + + // If there is a positive value then that value + 1 is the element missing + for(int i=0; i 0){ + returnList.add(i+1); + } + } + + return returnList; +} \ No newline at end of file diff --git a/FindMinMax.cs b/FindMinMax.cs new file mode 100644 index 00000000..73486ed8 --- /dev/null +++ b/FindMinMax.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Algorithms +{ + public class FindMinMax + { + //Time Complexity - 2*O(n) - Since we are doing 2 comparisions for each element in the array + // Space Complexity - O(1) - Since we are not using any extra space + public void FindMinMaxWithFourSteps(int[] nums) + { + var min = nums[0]; + var max = nums[0]; + + // For every element there are 2 comparisions - Total 2n steps + for(int i = 1; i < nums.Length; i++) + { + min = Math.Min(min, nums[i]); + max = Math.Max(max, nums[i]); + } + + Console.WriteLine("Min :{0}", min); + Console.WriteLine("Max: {0}", max); + } + + //Time Complexity - 3n/2 = 1.5 * O(n) - Since we have 3 comparisions and are iterating in pairs + //Space complexity - O(1) - Since we are not using any extra space + public void FindMinMaxWithThreeSteps(int[] nums) + { + var min = int.MaxValue; + var max = int.MinValue; + + // One comaprision between current element and next element, the other Two comparisions between Min and max and these elements + // Total 3 steps + + for (int i = 0; i < nums.Length - 1; i++) + { + if(nums[i] < nums[i + 1]) + { + min = Math.Min(nums[i], min); + max = Math.Max(nums[i + 1], max); + } + else + { + min = Math.Min(nums[i + 1], min); + max = Math.Max(nums[i], max); + } + } + + Console.WriteLine("Min :{0}", min); + Console.WriteLine("Max: {0}", max); + } + } +} diff --git a/GameOfLife.java b/GameOfLife.java new file mode 100644 index 00000000..4e717eec --- /dev/null +++ b/GameOfLife.java @@ -0,0 +1,81 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(m * n) since we are iterating through the matrix couple of times +//Space Complexity: O(1) since we are not taking any extra space + +public void gameOfLife(int[][] board) { + //live to die => -1 + //die to live => 2 + for(int i=0; i 3) { + board[i][j] = -1; + } + } else { + //Case-4 + if(livingCount(board, i, j) == 3) { + board[i][j] = 2; + } + } + } + } + + for(int i=0; i0) { + if(board[i-1][j] == 1 || board[i-1][j] == -1) count++; + } + + //right + if(j < ncols-1) { + if(board[i][j+1] == 1 || board[i][j+1] == -1) count++; + } + + //bottom + if(i < nrows-1) { + if(board[i+1][j] == 1 || board[i+1][j] == -1) count++; + } + + //left + if(j > 0) { + if(board[i][j-1] == 1 || board[i][j-1] == -1) count++; + } + + // top-left + if(i>0 && j > 0) { + if(board[i-1][j-1] == 1 || board[i-1][j-1] == -1) count++; + } + + //top-right + if(i>0 && j 0) { + if(board[i+1][j-1] == 1 || board[i+1][j-1] == -1) count++; + } + + //bottom-right + if(i < nrows-1 && j < ncols-1) { + if(board[i+1][j+1] == 1 || board[i+1][j+1] == -1) count++; + } + + return count; + } \ No newline at end of file From d523b841963fba8a78444457381072299729af3a Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Thu, 13 Aug 2026 01:54:00 -0400 Subject: [PATCH 2/2] Complete Array-2 Assignment --- DisappearedNumbers.java | 43 +++++++------- FindMinMax.cs | 57 ------------------- GameOfLife.java | 122 +++++++++++++++++----------------------- MaxAndMinArray.java | 30 ++++++++++ 4 files changed, 104 insertions(+), 148 deletions(-) delete mode 100644 FindMinMax.cs create mode 100644 MaxAndMinArray.java diff --git a/DisappearedNumbers.java b/DisappearedNumbers.java index 556356bc..5e0549d9 100644 --- a/DisappearedNumbers.java +++ b/DisappearedNumbers.java @@ -1,28 +1,31 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No +import java.util.*; -//Time Complexity : O(n) since we are iterating through the array linearly -//Space Complexity: O(1) since we are not taking any extra space except result array +//Approach: The idea is to calculate the index of the current element and making the element in that index to negative. +//At the end, iterate through the array to find if there are any positive elements in the original array and add them +// to the result. -public List findDisappearedNumbers(int[] nums) { - ArrayList returnList = new ArrayList<>(); - - if(nums == null || nums.length == 0) return returnList; - - // Iterate through the array and change the index corresponding to the element to a negative value - for(int i = 0; i < nums.length; i++){ - int index = Math.abs(nums[i]) - 1; - if(nums[index] > 0){ - nums[index] = -1 * nums[index]; +//Time Complexity : O(2n) +//Space Complexity: O(1) + +class DisappearedNumbers { + public List findDisappearedNumbers(int[] nums) { + + List ans = new ArrayList<>(); + + for (int i = 0; i < nums.length; i++) + { + int idx = Math.abs(nums[i]) - 1; + if (nums[idx] > 0){ + nums[idx]*= -1; } } - - // If there is a positive value then that value + 1 is the element missing + for(int i=0; i 0){ - returnList.add(i+1); + ans.add(i+1); } } - - return returnList; -} \ No newline at end of file + + return ans; + } +} diff --git a/FindMinMax.cs b/FindMinMax.cs deleted file mode 100644 index 73486ed8..00000000 --- a/FindMinMax.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Algorithms -{ - public class FindMinMax - { - //Time Complexity - 2*O(n) - Since we are doing 2 comparisions for each element in the array - // Space Complexity - O(1) - Since we are not using any extra space - public void FindMinMaxWithFourSteps(int[] nums) - { - var min = nums[0]; - var max = nums[0]; - - // For every element there are 2 comparisions - Total 2n steps - for(int i = 1; i < nums.Length; i++) - { - min = Math.Min(min, nums[i]); - max = Math.Max(max, nums[i]); - } - - Console.WriteLine("Min :{0}", min); - Console.WriteLine("Max: {0}", max); - } - - //Time Complexity - 3n/2 = 1.5 * O(n) - Since we have 3 comparisions and are iterating in pairs - //Space complexity - O(1) - Since we are not using any extra space - public void FindMinMaxWithThreeSteps(int[] nums) - { - var min = int.MaxValue; - var max = int.MinValue; - - // One comaprision between current element and next element, the other Two comparisions between Min and max and these elements - // Total 3 steps - - for (int i = 0; i < nums.Length - 1; i++) - { - if(nums[i] < nums[i + 1]) - { - min = Math.Min(nums[i], min); - max = Math.Max(nums[i + 1], max); - } - else - { - min = Math.Min(nums[i + 1], min); - max = Math.Max(nums[i], max); - } - } - - Console.WriteLine("Min :{0}", min); - Console.WriteLine("Max: {0}", max); - } - } -} diff --git a/GameOfLife.java b/GameOfLife.java index 4e717eec..3cc5c93e 100644 --- a/GameOfLife.java +++ b/GameOfLife.java @@ -1,81 +1,61 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No -//Time Complexity : O(m * n) since we are iterating through the matrix couple of times -//Space Complexity: O(1) since we are not taking any extra space +//Approach: The idea behind this solution is to iterate through the matrix using direction array and apply the rules to +//mark a particular element is dead or alive. To avoid collisions, in the first iteration mark the dead and alive with +// other any numbers other than 0 and 1. At the end, iterate through the matrix to replace with original 0 and 1. + +//Time Complexity: O(mxn) +//Space Complexity: O(1) +class GameOfLife +{ + int[][] dirs; + int m, n; + public void gameOfLife(int[][] board) { + + this.dirs = new int[][] {{-1, 1}, {-1,0}, {-1, -1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1,1}}; -public void gameOfLife(int[][] board) { //live to die => -1 //die to live => 2 for(int i=0; i 3) { - board[i][j] = -1; - } - } else { - //Case-4 - if(livingCount(board, i, j) == 3) { - board[i][j] = 2; - } - } - } + for(int j=0; j 3) { + board[i][j] = -1; + } + } else { + //Case-4 + if(livingCount(board, i, j) == 3) { + board[i][j] = 2; + } + } + } } - + for(int i=0; i0) { - if(board[i-1][j] == 1 || board[i-1][j] == -1) count++; - } - - //right - if(j < ncols-1) { - if(board[i][j+1] == 1 || board[i][j+1] == -1) count++; - } - - //bottom - if(i < nrows-1) { - if(board[i+1][j] == 1 || board[i+1][j] == -1) count++; - } - - //left - if(j > 0) { - if(board[i][j-1] == 1 || board[i][j-1] == -1) count++; - } - - // top-left - if(i>0 && j > 0) { - if(board[i-1][j-1] == 1 || board[i-1][j-1] == -1) count++; - } - - //top-right - if(i>0 && j 0) { - if(board[i+1][j-1] == 1 || board[i+1][j-1] == -1) count++; - } - - //bottom-right - if(i < nrows-1 && j < ncols-1) { - if(board[i+1][j+1] == 1 || board[i+1][j+1] == -1) count++; - } - - return count; - } \ No newline at end of file + int count = 0, m = board.length, n = board[0].length; + + for (int[] dir : dirs) + { + int r = i + dir[0]; + int c = j + dir[1]; + + if(r >= 0 && c >= 0 && r < m && c < n && (board[r][c] == 1 || board[r][c] == -1)) // Check boundaries + { + count++; + } + } + + return count; + } +} \ No newline at end of file diff --git a/MaxAndMinArray.java b/MaxAndMinArray.java new file mode 100644 index 00000000..e71522ec --- /dev/null +++ b/MaxAndMinArray.java @@ -0,0 +1,30 @@ +//Given an array of numbers of length N, find both the minimum and maximum. Follow up : Can you do it using less than 2 * (N - 2) comparison +//Time Complexity : O(n) and 2n comparisions +//Space Complexity: O(1) +//Approach: Find min and max between pairs of elements while comparing them with global min and max values to get the final min and max values +public class MaxAndMinArray { + + public int[] FindMaxAndMin(int[] arr) + { + //Validate the inputs + if (arr == null || arr.length == 0) return new int[]{-1, -1}; + + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; + + for (int i = 0; i < arr.length-1; i++) + { + if (arr[i] > arr[i+1]) + { + max = Math.max(max, arr[i]); + min = Math.min(min, arr[i+1]); + } + else + { + max = Math.max(max, arr[i+1]); + min = Math.min(min, arr[i]); + } + } + return new int[] {min, max}; + } +}