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
64 changes: 64 additions & 0 deletions ARR2_289_Game_of_Life.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**** Method 1 ****/
//Time Complexity: O(n^2)
//Space Complexity: O(1)

//Successfully submitted in LeetCode

//Traverse the arr, and at each index find the count of 1's if it is already 1 and count <2 or >3 then change it's value to 2 as we if alter it to "0" we may lose the state for others,else if is "0" and count is 3 we update the board value to 3. Later we convert all the 2 to 0 and 3 to 1 by traversing.

public class ARR2_289_Game_of_Life {

public void gameOfLife(int[][] board) {
//1 --> 0 : 2
//0 --> 1 : 3

for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
int count = count1s(board, i, j);
if (board[i][j] == 1) {
if (count < 2 || count > 3) {
board[i][j] = 2;
}
} else {
if (count == 3) {
board[i][j] = 3;
}
}
}
}

for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 2) board[i][j] = 0;
if (board[i][j] == 3) board[i][j] = 1;
}
}
}

public int count1s(int[][] board, int i, int j) {
int count = 0;

int mat[][] = {
{ -1, -1 },
{ -1, 0 },
{ -1, 1 },
{ 0, -1 },
{ 0, 1 },
{ 1, -1 },
{ 1, 0 },
{ 1, 1 },
};

for (int k = 0; k < mat.length; k++) {
int x = i + mat[k][0];
int y = j + mat[k][1];
if (x >= 0 && x < board.length && y >= 0 && y < board[i].length) {
if (board[x][y] == 1 || board[x][y] == 2) {
count++;
}
}
}

return count;
}
}
87 changes: 87 additions & 0 deletions ARR2_448_Find_All_Numbers_Disappeared_in_an_Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**** Method 1 ****/
//Time Complexity: O(n^2)
//Space Complexity: O(1)

//Successfully submitted in LeetCode

//Take two loops one from 1 to n and another to traverse the arr and check if ele from first loop is present or not. If not add to missing list. At the end return missing list.

/**** Method 2 ****/
//Time Complexity: O(n)
//Space Complexity: O(n)

//Successfully submitted in LeetCode

//Traverse the arr and store all it's values in set, later start another loop from 1 to n and check if set contains it or not. If not add to missing list. At the end return missing list.

/**** Method 3 ****/
//Time Complexity: O(n)
//Space Complexity: O(1)

//Successfully submitted in LeetCode

//ONLY IF WE CAN MODIFY THE GIVEN ARR. As all the elements present should generally be less than n, we can traverse the arr and make the index of the value as -ve, so in this way all elements in arr will have there index marked -ve. So at end we can traverse the array and add the missing items(which values are not -ve) to list.

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

public class ARR2_448_Find_All_Numbers_Disappeared_in_an_Array {

/**** Method 1 ****/
public List<Integer> findDisappearedNumbers1(int[] nums) {
List<Integer> ans = new ArrayList<>();

for (int i = 1; i <= nums.length; i++) {
boolean flag = false;
int idx = 0;
while (idx < nums.length) {
if (nums[idx] == i) {
flag = true;
break;
}
}
if (!flag) {
ans.add(i);
}
}
return ans;
}

/**** Method 2 ****/
public List<Integer> findDisappearedNumbers2(int[] nums) {
List<Integer> ans = new ArrayList<>();
HashSet<Integer> set = new HashSet<>();

for (int i : nums) {
set.add(i);
}

for (int i = 1; i <= nums.length; i++) {
if (!set.contains(i)) {
ans.add(i);
}
}

return ans;
}

/**** Method 3 ****/
public List<Integer> findDisappearedNumbers3(int[] nums) {
List<Integer> ans = new ArrayList<>();

for (int i = 0; i < nums.length; i++) {
if (nums[Math.abs(nums[i]) - 1] > 0) {
nums[Math.abs(nums[i]) - 1] *= -1;
}
}

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

return ans;
}
}