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
26 changes: 26 additions & 0 deletions FindAllNumbersMissingInArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity : O(n).
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Approach : If we sort the Array, as the condition is 1 to n numbers, the element should be at i+1 index. Assuming this logic, we
// iterate through array and make the index's value-1 index to be negative. Which means all the numbers already present will be negative.
// Now we iterate again, and the index+1 of the positive numbers ill give the result.

class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();

for(int i=0;i<nums.length;i++){
int val = Math.abs(nums[i]);
if(nums[val-1] > 0){
nums[val-1] = -nums[val-1]; //marking the value that is supposed to be in this position to negative
}
}

for(int i=0;i<nums.length;i++){
if(nums[i] > 0){ //non-negative number's index will be the ones that are not visited.
res.add(i+1);
}
}
return res;
}
}
54 changes: 54 additions & 0 deletions GameOfLife.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Time Complexity : O(m*n).
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Approach : As per the problem and given conditions, we change the alive to dead ones to 2 and dead to alive ones to 3. As all
// operations happen simultaneously according to the problem, we shouldn't consider the updated values so we maintain 2 or 3 for updated
// ones. We first calculate the alives and perform the logic on the count, iterate the array again and transform 2 and 3 to alive or
// dead values in the matrix.

class Solution {
int m, n;

public void gameOfLife(int[][] board) {
this.m = board.length;
this.n = board[0].length;
for (int i=0;i<m;i++){
for(int j=0;j<n;j++){
int alivesCount = CountAlives(board, i, j);
if(board[i][j] == 1 && (alivesCount < 2 || alivesCount > 3)){ //as per the condition in problem
board[i][j] = 2; // alive to dead.
}
if(board[i][j] == 0 && alivesCount == 3){
board[i][j] = 3;// dead to alive
}
}

}
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){// iterate again to make 2's as 0 and 3 as 1
if(board[i][j] == 2){
board[i][j] = 0;
}else if(board[i][j] == 3){
board[i][j] = 1;
}
}
}

}

public int CountAlives(int[][] board, int i, int j) {
//All 8 possible directions are stored.
int directions[][] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, 1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 } };
int count = 0;
for (int[] dir : directions) {
int row = i + dir[0];
int col = j + dir[1];

if (row >= 0 && row < m && col >= 0 && col < n) { // row and column are within borders.
if (board[row][col] == 1 || board[row][col] == 2) //as we changed to 2 for alive to dead.
count++;
}
}
return count;
}
}
33 changes: 33 additions & 0 deletions MinAndMaxInArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity : O(n).
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Approach : Comparison by pairs approach helps in reducing the comparisons that checking min and max at each value. We consider two
// elements in the array and based on the bigger value we compare with existing max and voce versa for min. If array size is odd I was
// missing the last element, so I have added an additional condition for it as well.

class Solution {
public ArrayList<Integer> getMinMax(int[] arr) {
// code Here
ArrayList<Integer> res = new ArrayList<>();
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int i=0;
for(i=0;i<arr.length-1;i+=2){ //increment by 2 as we are comparing pairs
if(arr[i] < arr[i+1]){
min = Math.min(min,arr[i]);
max = Math.max(max,arr[i+1]); //as i+1 is bigger, compare with previous max
} else{
min = Math.min(min,arr[i+1]); //as i+1 is smaller, compare with previous min
max = Math.max(max,arr[i]);
}
}
if(i == arr.length-1){ //additional check for last element of odd length arrays
min = Math.min(min,arr[i]);
max = Math.max(max,arr[i]);
}
res.add(min);
res.add(max);
return res;

}
}