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
19 changes: 19 additions & 0 deletions find_disappered_numbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n= nums.length;
for(int i=0;i<n;i++){
int num= nums[i];
int idx= Math.abs(num)-1;
if(nums[idx]>0){
nums[idx]*=-1;
}
}
List<Integer> result = new ArrayList<>();
for(int i=0;i<n;i++){
if(nums[i]>0){
result.add(i+1);
}
}
return result;
}
}
45 changes: 45 additions & 0 deletions game_of_life.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
int[][] dirs;
int m;
int n;
public void gameOfLife(int[][] board) {
//1-->0==2
//0-->1 == 3
this.m = board.length;
this.n = board[0].length;
this.dirs = new int[][]{{-1,-1}, {-1,0}, {-1, 1}, {0, -1}, {0, 1}, {1,-1}, {1,0}, {1,1}};
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
int count= getCount(board,i,j);
if(board[i][j]==0 && count==3){
board[i][j]=3;
}
else if(board[i][j]==1 &&(count < 2 || count >3)){
board[i][j]=2;
}
}
}
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;
}
}
}
}
private int getCount(int[][] board,int i, int j){
int count =0;
for(int[] dir:dirs){
int r= i+dir[0];
int c= j+dir[1];
if(r>=0 && c>=0 && r<m && c<n){
if(board[r][c] == 1 || board[r][c] == 2) count++;
}
}
return count;
}

}
37 changes: 37 additions & 0 deletions min_max.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public class Problem3 {

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