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
40 changes: 40 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//Problem - LeetCode 448 Find All Numbers Disappeared in an Array
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Not able to come up with this logic on my own

// Your code here along with comments explaining your approach
import java.util.*;

//Approach - mark the numbers which are already there as negative. So in the second traversal we can iddentify the non negative numbers and their index + 1 should be the missing number

public class Problem1 {
public List<Integer> findDisappearedNumber(int [] nums){
List<Integer> result = new ArrayList<>();
int n = nums.length;

//first for the given number nums[i] -- see what should be its actual index
//for e.g. nums[i] = 4 --> its index should be 3; So mark nums[3] as negative (*-1)

for(int i = 0; i < n; i++){
int idx = Math.abs(nums[i]) - 1;
if(nums[idx] < 0) continue; //number already marked as negative
else nums[idx] *= -1;
}

// In second traversal - whatever are the numbers are non negative, for them add their idx + 1 into the result

for(int i = 0; i < n; i++){
if(nums[i] < 0) nums[i] *= -1; // to keep original array as intact
else result.add(i+1); //number at that idx is non negative so idx + 1 would be missing so add it in the list.
}
return result;
}

public static void main(String[] args) {
Problem1 demo = new Problem1();
int [] nums = {4,3,2,7,8,2,3,1};
System.out.println(demo.findDisappearedNumber(nums));
}
}
70 changes: 70 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Time Complexity :O (3N/2)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Problem not in leetcode
// Any problem you faced while coding this : Struggled to get the pairs logic for this problem

//We will form pairs of two elements and compare the elements with each other. The greater number needs to be compared with max and the smaller number needs to be compared with min. So, with this approach we don't compare each element for max and min unnecessarily


// Your code here along with comments explaining your approach
public class Problem2 {
public void compareElements(int[]nums){
int max = Integer.MIN_VALUE;
int min = Integer.MIN_VALUE;
int i;

if(nums.length == 0 || nums == null) System.out.println("Array is empty");

if(nums.length == 1){
max = nums[0];
min = nums[0];
}

if(nums.length % 2 != 0){
//array is odd. So consider first element as min and max both and start forming pairs
max = min = nums[0];
i =1; // start with 1st element of array
}

else{
if (nums[0] < nums[1]){
max = nums[1];
min = nums[0];
}
else {
max = nums[0];
min = nums[1];
}
i = 2; //start from index 2
}

while(i < nums.length -1){
if(nums[i] < nums[i+1]){
if(nums[i+1] > max) max = nums[i+1];
if(nums[i] < min) min = nums[i];
}
else{
//nums[i] is greater
if(nums[i] > max) max = nums[i];
if(nums[i+1] < min) min = nums[i+1];
}
i += 2;
}

System.out.println("Array size : " + nums.length);
System.out.println("Max : " + max);
System.out.println("Min : " + min);
}
public static void main(String[] args) {
Problem2 demo = new Problem2();

int [] nums = {3, 1, 4, 1, 5, 9};
demo.compareElements(nums);

int [] nums2 = {7, 2, 8, 3, 6};
demo.compareElements(nums2);

int [] nums3 = {1, 2, 3, 4, 5, 6};
demo.compareElements(nums3);
}
}
67 changes: 67 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//Problem - 289. Game of Life
// Time Complexity : O(m*n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : Could not solve it on my own and faced syntax issues
public class Problem3 {
int[][] dirc;
int m , n;

public void gameOfLife(int[][] board){
this.m = board.length;
this.n = board[0].length;
//defining the directions
this.dirc = new int[][] {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,1},{1,0},{1,-1}};

//if prev dead now alive then change it with number - 11
//if prev alive now dead then change it with number - 13

for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
//this function will give us all the neighbouring 1s for each element. Pass the current element in it and it will give neighbouring 1s. Based on which it will be decided whether the current element will live or die.

int count = getCount(board, i, j);

if(board[i][j] == 0){
if(count == 3) board[i][j] = 11;
}
else if(board[i][j] == 1){
if(count <2 || count >3) board[i][j] = 13;
}
}
}

//get the final array
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 11) board[i][j] = 1;
else if(board[i][j] == 13) board[i][j] = 0;
}
}


for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
System.out.print(board[i][j]);
}
System.out.println();
}
}

private int getCount(int[][]board, int idx1, int idx2){
int count = 0;
for(int[] dir : dirc){
int r = idx1 +dir[0];
int c = idx2 + dir[1];

if( r >= 0 && c >= 0 && r<m && c<n && (board[r][c] == 1 || board[r][c] == 13)) count++;
}
return count;
}

public static void main(String[] args) {
Problem3 demo = new Problem3();
int [][] board = {{0,1,0},{0,0,1},{1,1,1},{0,0,0}};
demo.gameOfLife(board);
}
}