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

//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.

//Time Complexity : O(2n)
//Space Complexity: O(1)

class DisappearedNumbers {
public List<Integer> findDisappearedNumbers(int[] nums) {

List<Integer> 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;
}
}

for(int i=0; i<nums.length; i++){
if(nums[i] > 0){
ans.add(i+1);
}
}

return ans;
}
}
61 changes: 61 additions & 0 deletions GameOfLife.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

//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}};

//live to die => -1
//die to live => 2
for(int i=0; i<board.length; i++) {
for(int j=0; j<board[0].length; j++) {
if(board[i][j] == 1) {
//Case-1 or Case-3
if(livingCount(board, i, j) < 2 || livingCount(board, i, j) > 3) {
board[i][j] = -1;
}
} else {
//Case-4
if(livingCount(board, i, j) == 3) {
board[i][j] = 2;
}
}
}
}

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] = 0;
} else if(board[i][j] == 2) {
board[i][j] = 1;
}
}
}
}

private int livingCount(int[][] board, int i, int j) {
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;
}
}
30 changes: 30 additions & 0 deletions MaxAndMinArray.java
Original file line number Diff line number Diff line change
@@ -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};
}
}