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
32 changes: 32 additions & 0 deletions FindDisappearedNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.*;
class Solution {

// TC - O(n)
// SC - O(1)

/*
We are updating the input array itself to not use any extra space.
*/
public List<Integer> findDisappearedNumbers(int[] nums) {
// iterate array, get value at index and subtract 1 (nums[i] - 1) to get the index at which the number should be present (if it was sorted) and multiply num by -1
for (int i = 0; i < nums.length; i++) {
// take absolute value of the element (in case we mofified it with multiplying by -1)
int index = Math.abs(nums[i]) - 1;
// if valu at index is greater than 0, then multiply by -1 (this marks that the number index + 1 exists)
if (nums[index] > 0) {
nums[index] *= -1;
}
}

List<Integer> list = new ArrayList<>();

// iterate the array and if number at index is not -ve, it means that the number (index + 1) does exist in the array
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
list.add(i + 1);
}
}

return list;
}
}
64 changes: 64 additions & 0 deletions GameOfLife.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class Solution {
// TC - O(n)
// SC - O(1)
public void gameOfLife(int[][] board) {
int m = board.length;
int n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// get the neighbouring count of live cells
int count = countLiveCells(board, i, j, m, n);

// if element at index is 1 but count of live cells < 2 or >=4
// then set the element value to 2 (we choose 2 so that we can modify the same input array
// to find the next state of matrix)

// so if value is being updated from 0 -> 1 we set it to -1
// if value is updated from 1 -> 0 we set it as 2
if (board[i][j] == 1) {
// set value as 2 if 1 -> 0
if (count < 2 || count >= 4) {
board[i][j] = 2;
}
}
// if element at index is 0 but count of live cells is 3
// then set the element value to -1
if (board[i][j] == 0 && count == 3) {
// set value as -1 if 0 -> 1
board[i][j] = -1;
}
}
}

// to reset the input array value to the original value
// if value is 2 -> set it as 0, if value is -1 -> set it as 1
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == -1) {
board[i][j] = 1;
}
if (board[i][j] == 2) {
board[i][j] = 0;
}
}
}
}

private int countLiveCells(int[][] board, int i, int j, int m, int n) {
int count = 0;
// we take an array of directions that we need to add to indexes to find their neighbouring cells
int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};

for (int[] dir : dirs) {
int nr = i + dir[0];
int nc = j + dir[1];

// if index is not out of bounds of the matrix and value is > 0 (1 or 2), then incr the count of live cells
if (nc >= 0 && nc < n && nr >= 0 && nr < m && board[nr][nc] > 0) {
count++;
}
}

return count;
}
}
42 changes: 42 additions & 0 deletions MinAndMax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.ArrayList;
import java.util.Arrays;

public class MinAndMax {
// TC = O(3n/2)
// SC = O(1)
public ArrayList<Integer> findMinMax(int[] arr) {
int min, max, i;

// to handle array with odd length, initialize min and max with arr[0]
// and iterate rest of the array in the same way
if (arr.length % 2 == 1) {
min = max = arr[0];
i = 1;
} else {
// else set min and max from the first two indexes
if (arr[0] < arr[1]) {
min = arr[0];
max = arr[1];
} else {
min = arr[1];
max = arr[0];
}
i = 2;
}

// We process elements in pairs to minimize the number of comparisons
while (i < arr.length - 1) {
//compare the two indexes to find the lower number
if (arr[i] < arr[i + 1]) {
min = Math.min(min, arr[i]); // lower number is used for min comparison
max = Math.max(max, arr[i + 1]); // higher value number is used to compare with max value
} else {
min = Math.min(min, arr[i + 1]); // lower number is used for min comparison
max = Math.max(max, arr[i]); // higher value number is used to compare with max value
}
i += 2;
}

return new ArrayList<Integer>(Arrays.asList(min, max));
}
}