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
60 changes: 60 additions & 0 deletions src/DisapperedNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Problem -Find All Numbers Disappeared in an Array
Approach1 - •We use each number in the array to mark its corresponding index negative.
•If any index remains positive, it means that number was missing from the array.
•So we collect all those index+1 values as the result.
time - O(n)
space - O(1)
Approach2 - •
We first put all numbers from the array into a set for quick lookup.
•Then we go from 1 to n and check which number isn’t in the set.
•If a number is missing, we add it to our result list.
time - O(n)
space - O(n)
*/

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class DisapperedNumbers {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> result = new ArrayList<>();
for(int i = 0; i < nums.length; i++){
int temp = Math.abs(nums[i]) -1;
if(nums[temp] > 0){
nums[temp] *= -1; // change the number to negative only if the number is not negative in first place
}
}
for(int i = 0; i < nums.length; i++){
if(nums[i] > 0){ // if the modified number in the array is not negative add it to the result
result.add(i+1);
}
else {
nums[i] *= -1; // to change the state back to original one
}
}
return result;
}
public List<Integer> findDisappearedNumbersUsingSet(int[] nums) {
HashSet<Integer> set = new HashSet<>();
List<Integer> result = new ArrayList<>();
for(int i = 0; i < nums.length; i++){
set.add(nums[i]); // add all the numbers to a set
}
for(int i = 1; i <= nums.length; i++){ // iterate through the range of integers which is length of the array
if(!set.contains(i)){ //if the number doesn't found in set add it to the result
result.add(i);
}
}
return result;
}
public static void main(String[] args) {
DisapperedNumbers d = new DisapperedNumbers();
List<Integer> res = d.findDisappearedNumbers(new int[]{4,3,2,7,8,2,3,1});
System.out.println(res);
List<Integer> res2 = d.findDisappearedNumbersUsingSet(new int[]{4,3,2,7,8,2,3,1});
System.out.println(res2);

}
}
83 changes: 83 additions & 0 deletions src/GameOfLife.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Problem - Game of Life
Approach - We go cell by cell and count how many live neighbors it has.
We mark transitions with temporary values: 2 (alive → dead) and 3 (dead → alive).
then, we finalize the board by converting 2 → 0 and 3 → 1.
Time Complexity - O(m*n)
Space Complexity - O(n)
*/

import java.util.Arrays;

public class GameOfLife {
public void gameOfLife(int[][] board) {
// 1 ----> 0 == 2
// 0---> 1 == 3
int m = board.length;
int n = board[0].length;
//checking the neighbors to see if the conditions given in the problem to make it alive,
// if we are transitioning to 1-->0 we are marking it as 2 and if we are transitions from 0-->1 we are marking it as 3
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int count = countAlive(board, i, j);
if ((board[i][j] == 1) && (count < 2 || count > 3)) {
board[i][j] = 2;
}
if ((board[i][j] == 0) && count == 3) {
board[i][j] = 3;
}
}
}
// Iterating again through the array to make to roolback the previous transitions which are marked as 2 and 3 to their original values
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 2) {
board[i][j] = 0;
}
if (board[i][j] == 3) {
board[i][j] = 1;
}
}
}
}
public int countAlive(int[][] board, int i, int j) {
int count = 0;
int[][] dirs = new int[][]{{-1,-1}, {-1,0}, {-1, 1}, {0, -1}, {0, 1}, {1,-1}, {1,0}, {1,1}};
for(int []dir:dirs) {
int r = i + dir[0];
int c = j + dir[1];
if (r >= 0 && c >= 0 && r < board.length && c < board[0].length) {
if (board[r][c] == 1 || board[r][c] == 2) count++;
}
}
return count;
}
public static void main(String[] args) {
GameOfLife gol = new GameOfLife();

int[][] board = {
{0, 1, 0},
{0, 0, 1},
{1, 1, 1},
{0, 0, 0}
};

gol.gameOfLife(board);

// Expected after 1 step:
// [
// [0,0,0],
// [1,0,1],
// [0,1,1],
// [0,1,0]
// ]
int[][] expected = {
{0, 0, 0},
{1, 0, 1},
{0, 1, 1},
{0, 1, 0}
};

System.out.println(Arrays.deepEquals(board, expected));
}
}
49 changes: 49 additions & 0 deletions src/MaxMin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Problem - An array of numbers of length N is given , you need to find the minimum and maximum. try doing this in less than 2* (N-2) comparisons
Approach - We compare the first one or two elements to initialize the min and max.
Then we scan the array in pairs and update min and max by comparing within the pair first.
•This reduces the number of comparisons to about 1.5 * n, making it efficient.
*/

import java.util.Arrays;

public class MaxMin {
public int[] findMinAndMax(int[] nums) {
int n = nums.length;
int min, max;
int i;
// Handle first pair (or first element if odd length)
if (n % 2 == 0) {
if (nums[0] < nums[1]) {
min = nums[0];
max = nums[1];
} else {
min = nums[1];
max = nums[0];
}
i = 2;
} else {
min = max = nums[0];
i = 1;
}

// Process pairs
while (i < n - 1) {
if (nums[i] < nums[i + 1]) {
min = Math.min(min, nums[i]);
max = Math.max(max, nums[i + 1]);
} else {
min = Math.min(min, nums[i + 1]);
max = Math.max(max, nums[i]);
}
i += 2;
}

return new int[]{min, max};
}
public static void main(String[] args) {
MaxMin obj = new MaxMin();
System.out.println(Arrays.toString(obj.findMinAndMax(new int[]{3, 5, 4, 1, 9})));
System.out.println(Arrays.toString(obj.findMinAndMax(new int[]{22, 14, 8, 17, 35, 3})));
}
}