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
46 changes: 46 additions & 0 deletions MinMaxFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.ArrayList;
import java.util.Arrays;

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

// Initialize min and max
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);
}

public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(3, 5, 4, 1, 9));
ArrayList<Integer> result = new ArrayList<>();
findMinMax(arr, result);
System.out.println(result.get(0) + " " + result.get(1));
}
}
28 changes: 28 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.ArrayList;
import java.util.List;

class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> li = new ArrayList<>();
int n=nums.length;
for(int i=0;i<n;i++){
int idx = Math.abs(nums[i])-1;
//marking negative values in list
if(nums[idx]>0){
nums[idx]=nums[idx]*-1;
}
}
for(int i=0;i<n;i++){
//for positive value add the the next corresponding index number in list
if(nums[i]>0){
li.add(i+1);
}
//marking negative values back to positive
else{
nums[i]=nums[i]*-1;
}

}
return li;
}
}
50 changes: 50 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Solution {
public void gameOfLife(int[][] board) {
int m=board.length;
int n=board[0].length;

//first pass
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
int count = countAlive(board, i ,j);
//1-->0 maps to 2
if(board[i][j]==1 && (count<2 || count>3)){
board[i][j]=2;
}
//0-->1 maps to 3
if(board[i][j]==0 && count==3 ){
board[i][j]=3;
}
}
}
//second pass
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
//2 maps backs to 0
if(board[i][j]==2){
board[i][j]=0;
}
//3 maps back to 1
if(board[i][j]==3){
board[i][j]=1;
}
}
}
}

private int countAlive(int[][] board, int i, int j){
int count=0; // up down right left
int dirs[][]= new int [][] {{0,1},{0,-1},{1,0},{-1,0},{-1,1},{1,1},{-1,-1},{1,-1}};

for(int[] dir: dirs){
//nr= nieghbouring row , nc=nieghbouring column
int nr=i+dir[0];
int nc=j+dir[1];
if(nr>=0 && nc>=0 && nr<board.length && nc<board[0].length &&
(board[nr][nc]==1 || board[nr][nc]==2)){
count++;
}
}
return count;
}
}