Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Since 1 <= nums[i] <= n all the elements of the array are positive. so the idea is to temporarily
// multiply the element in the (nums[i]-1)th index with -1.This change of sign of (nums[i]-1)th index element
//indicates that element is present in array. Once the array iteration is done , the positve elements index+1 are the missing numbers
// Time Complexity: O(n)
// Space complexity: O(1)
class Problem1 {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> result = new ArrayList<>();
for(int i = 0; i<nums.length; i++){
int idx = Math.abs(nums[i])-1; // get the absolute value if there is negative number
if(nums[idx]>0){
nums[idx] *= -1;
}
}
for(int i = 0; i<nums.length; i++){
if(nums[i]>0){
result.add(i+1); // +1 because ex: 4 is stored in index 3
}
}
for(int i = 0; i<nums.length; i++){
if(nums[i] < 0){
nums[i] *= -1;
}
}
return result;
}
}
38 changes: 38 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Time Complexity: O(n)
// Space Complexity: O(1)
// The idea here is to reduce the number of comparisions by using pairwise comparison technique for finding min and max
// Total comparisons:
// For Even n: 3 × (n/2) = 1.5n
// For Odd n: 3 × ((n-1)/2) + 2
class Problem2 {
public ArrayList<Integer> getMinMax(int[] arr) {
ArrayList<Integer> res = new ArrayList<>();
int min = arr[0];
int max = arr[0];
int length = arr.length;
int remainder = length%2;
// find number of times we have to iterate based on length of array
// if length is even all iterations. If length is odd skip the ;ast iteration
// to avoid index out of bound
int iterations = remainder == 0 ? length : length-1;

for(int i=0; i<iterations; i = i+2){
if(arr[i] > arr[i+1]) {
min = Math.min(min, arr[i+1]);
max = Math.max(max,arr[i]);
} else {
min = Math.min(min, arr[i]);
max = Math.max(max,arr[i+1]);
}
}

// extra 2 comparisions for last element in odd length case.
if(remainder!=0){
min = Math.min(min, arr[length-1]);
max = Math.max(max, arr[length-1]);
}
res.add(min);
res.add(max);
return res;
}
}
57 changes: 57 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Time Complexity: O(m*n)
// Space Complexity: O(1)
// We go cell by cell and count how many live neighbors it has.
// based on the given conditions we mark transitions with temporary values: 2 (alive → dead) and 3 (dead → alive).
// After iterating all the cells we convert 2 ->0 and 3 ->1

class Problem3 {
public void gameOfLife(int[][] board) {
int m = board.length; //rows
int n = board[0].length; // columns

// alive to dead 1->0 : 2
// dead to alive 0->1: 3
for (int i = 0; i<m; i++){
for(int j=0; j<n;j++) {
int liveCellCount = getLiveCellCount(board, i, j, m, n);
// make it dead cell
if(board[i][j] == 1 && (liveCellCount < 2 || liveCellCount > 3)) {
board[i][j] = 2;
}
// make it live cell
if(board[i][j] == 0 && liveCellCount == 3) {
board[i][j] = 3;
}
}
}

// change back to 2 and 3 to actual dead or live cells
for (int i = 0; i<m; i++){
for(int j=0; j<n;j++) {
if(board[i][j] == 2) {
board[i][j] = 0; // it is changed from 1->0
}
if(board[i][j] == 3) {
board[i][j] = 1; // it is changed from 0->1
}
}
}

}

private int getLiveCellCount(int[][] board, int row, int column, int totalRows, int totalColumns){
int count = 0;
// initialise the direction matrix for all 8 directions
int [][] dirs = new int[][]{{-1,0},{1,0},{0,1},{0,-1},{-1,1},{1,1},{-1,-1},{1,-1}};
// iterate over direction array to get new adjacent cell live and find if new cell is live or not
for(int[] dir : dirs){
int nr = row+dir[0];
int nc = column+dir[1];
// check for index out of bound and live cell
if(nr >=0 && nr < totalRows && nc >=0 && nc < totalColumns && (board[nr][nc] ==1 || board[nr][nc] == 2)){
count++;
}
}
return count;
}
}