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
28 changes: 28 additions & 0 deletions find-all-numbers-disappeared-in-an-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english : For each number in the array, mark its corresponding index as negative to indicate it exists.
// After marking, any index that remains positive represents a number that is missing. Collect all such missing numbers and return them.



class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> output = 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) {
output.add(i+1);
}

}
return output;
}
}
49 changes: 49 additions & 0 deletions game-of-life.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// TimeComplexity: O(mn)
// SpaceComplexity: O(1)
// Explanation: We iterate through each cell, count its 8 neighbors, and mark state transitions in-place using temporary values (2 and 3) to preserve original states during computation.
// After the first pass, we convert the temporary states to their final values in a second pass.

class Solution {
public void gameOfLife(int[][] board) {
// 1-->0 ==2;
// 0-->1 ==3;
int m = board.length;
int n = board[0].length;
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; // live → dead
} else if (board[i][j] == 0 && count == 3) {
board[i][j] = 3; // dead → live
}

}
}

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

private int countAlive(int[][] board, int i, int j) {

int count =0;
int[][] dir = {{-1, -1}, {-1, 0}, {-1, 1},{ 0, -1},{ 0, 1},{ 1, -1}, { 1, 0}, { 1, 1}};
for (int k =0; k<dir.length; k++) {
int newi = i+dir[k][0];
int newj = j+dir[k][1];
if(newi > -1 && newi < board.length && newj >-1 && newj < board[0].length && (board[newi][newj] == 1 || board[newi][newj] == 2)){
count+=1;
}
}

return count;
}
}
42 changes: 42 additions & 0 deletions maximum-and-minimum-in-an-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Three line explanation of solution in plain english : Initialize min and max using the first one or two elements depending on array size.
// Iterate through the rest of the array in pairs, updating min and max efficiently. Return a list containing the final minimum and maximum values.

class Solution {
public static List<Integer> findMinMax(ArrayList<Integer> arr) {
List<Integer> result = new ArrayList<>();
int n = arr.size();
int mini, maxi, i;

if (n % 2 == 1) {
mini = maxi = arr.get(0);
i = 1;
} else {
if (arr.get(0) < arr.get(1)) {
mini = arr.get(0);
maxi = arr.get(1);
} else {
mini = arr.get(1);
maxi = arr.get(0);
}
i = 2;
}

// Process elements in pairs
while (i < n - 1) {
if (arr.get(i) < arr.get(i + 1)) {
mini = Math.min(mini, arr.get(i));
maxi = Math.max(maxi, arr.get(i + 1));
} else {
mini = Math.min(mini, arr.get(i + 1));
maxi = Math.max(maxi, arr.get(i));
}
i += 2;
}

result.add(mini);
result.add(maxi);
return result;
}
}