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
22 changes: 22 additions & 0 deletions Find-all-numbers-disappeared-in-an-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int m = nums.length;
List<Integer> list = new ArrayList<>();
for(int j=0;j<m;j++){
int x = Math.abs(nums[j]);
int y = nums[x-1];
if(y>0){
nums[x-1] *= -1;
}

}
for(int j=0;j<m;j++){
if(nums[j]>0){
list.add(j+1); //add the indices
}else{
nums[j] *= -1; //make the -ve elements as before to return as the original array
}
}
return list;
}
}
44 changes: 44 additions & 0 deletions Game-of-life.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
//1. 1-> <2 -> 0 ---2
//2. 1-> 2or3 -> 1
//3. 1-> >3 -> 0 --2
//4. 0-> ==3 -> 1 ---3
public void gameOfLife(int[][] board) {
int m = board.length;
int n = board[0].length;
int[][] dirs = new int[][]{{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,1},{1,-1}};
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
int countAlives = countLives(board, dirs, i,j,m,n);
if(board[i][j] == 0 && countAlives == 3){
board[i][j] = 3;
}
if(board[i][j] == 1 && (countAlives < 2 || countAlives > 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; //new state
}else if(board[i][j]==3){
board[i][j] = 1; //new state
}
}

}

}
private int countLives(int[][] board, int[][] dirs, int i, int j, int m, int n){
int count = 0;
for(int[] dir:dirs){
int nr = i + dir[0];
int nc = j + dir[1];
if(nr>=0 && nc>=0 && nr<m && nc<n && (board[nr][nc]==1 || board[nr][nc]==2)){
count++;
}
}
return count;
}
}
16 changes: 16 additions & 0 deletions Minimum-and-maximum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int[] minMax(int[] nums){
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
for(int i=0;i<n-2;i+2){
if(nums[i]> nums[i+1]){
min = Math.min(min, nums[i+1]);
max = Math.max(max, nums[i]);
}else{
min = Math.min(min, nums[i]);
max = Math.max(max, nums[i+1]);
}
}
return new int[]{min, max};
}
}
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# Array-2

## Problem1 (https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)

* nums that are not there need to be return
* Bruteforce - boolean array. Whichever element is present, value is True, else it is False. But it is taking additional space. To avoid that use optimal approach.
* Optimal - Use Temporaray state pattern, whichever element is present, at what index that element would be present make it -ve. If it was already -ve take absolute value of that, then change to -ve. If its duplicate element, don't do anything, Let the -ve be -ve only.

## Problem2
Given an array of numbers of length N, find both the minimum and maximum. Follow up : Can you do it using less than 2 * (N - 2) comparison
* Bruteforce - check every element, it will have 2n comparisons.
* Optimal consider pair, from that pair choose min & max. Do it for N/2 pairs. it will have N-2 comparison for min & N-2 for max


## Problem3 (https://leetcode.com/problems/game-of-life/)
* Bruteforce - we need to maintain extra apace for original state.
* Optimal - 1st & 4th condition where states are changing use Temporaray state pattern. Ex- 1-> <2 -> 0 ---2, 1-> >3 -> 0 --2, 0-> ==3 -> 1 ---3
* func countLives - to count 1s in all 8 directions