Skip to content
4 changes: 2 additions & 2 deletions src/HelloWorld.java → src/day1/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
public class HelloWorld {
package day1;

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello Competitive Programming");
}
}
33 changes: 33 additions & 0 deletions src/day1/MergeTwoSortedArrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day1;


/*

In m size of array add n more slots with number zero


m_arr = m - 1;
n_arr = n - 1;
last = m + n - 1;

compare m[m_arr] with n[n_arr]
if m[m_arr] > n[n_arr]
m[last] = m[m_arr]
m_arr--;
last--;
else
m[last] = n[n_arr]
n_arr--;
last

put all the elements of n size array in m and decrement last;


*/
public class MergeTwoSortedArrays {


public static void main(String[] args) {

}
}
40 changes: 40 additions & 0 deletions src/day1/MinimumPlatforms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package day1;

import java.util.Arrays;

/**
* Sort the arrival time in ascending order
* Sort the departure time in ascending order
* n is size of array
*
* platform = 1
* total platform;
*
* start with index 1 of arrival array with pointer name i
* j = index 0 of departure array
*
* run a loop i < n && j < n {
* if(arr[i] <= dep[j]) {
* platform++;
* i++;
* } else if(arr[i] > dep[j] {
* platform--;
* j++;
* }
*
* if(platform > total platform) {
* total platform = platform;
* }
*
* return total_platform;
* }
*/
public class MinimumPlatforms {




public static void main(String[] args) {

}
}
55 changes: 55 additions & 0 deletions src/day1/SearchInSortedMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package day1;

import java.util.Scanner;

public class SearchInSortedMatrix {

public static boolean isExist(int [][] matrix, int targetValue) {

//Start from top right
//if greater than target go to left
//if smaller then go to down

int i = 0;
int j = matrix[0].length - 1;

while (i < matrix.length && j >= 0) {
if(matrix[i][j] == targetValue) {
return true;
}

if(matrix[i][j] > targetValue) {
j--; //left column
} else {
i++; // next row
}
}

return false;
}


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows");
int r = sc.nextInt();
System.out.println("Enter number of cols");
int c = sc.nextInt();
int [][] matrix = new int[r][c];

for(int i = 0; i < r; i++) {
System.out.println("Enter elements for row" + i);
for(int j = 0; j < c; j++) {
int element = sc.nextInt();
matrix[i][j] = element;
}
}

System.out.println("Enter the value to be searched");

int target = sc.nextInt();

System.out.println(isExist(matrix, target));
}
}
67 changes: 67 additions & 0 deletions src/day1/Sort012.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package day1;

/**
* Use three pointers
*
* left = 0
* right = length - 1;
* mid = 0;
*
*
* run a loop from mid to high
*
* if(a[mid] == 0) {
* swap with left
* left++;
* mid++;
* } else id( a[mid] == 1) {
* mid++;
* } else {
* swap with right
* right--;
* }
*/
public class Sort012 {


public static void sorting(int [] arr) {
int left = 0;
int right = arr.length - 1;
int mid = 0;
while(mid <= right) {
if(arr[mid] == 0) {
int temp = arr[left];
arr[left] = arr[mid];
arr[mid] = temp;
left++;
mid++;
} else if(arr[mid] == 1) {
mid++;
} else {
int temp = arr[mid];
arr[mid] = arr[right];
arr[right] = temp;
right--;
}
}
}


private static void printArray(int [] arr) {
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}

public static void main(String[] args) {


int [] arr = {0, 2, 1, 1, 2, 1, 0, 0, 2, 1, 0, 1, 2, 0, 0, 1, 2};

sorting(arr);

printArray(arr);


}
}
65 changes: 65 additions & 0 deletions src/day1/TrappingRainWater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package day1;

/**
*
* T.C = O(n)
* S.C = O(n)
* create left and right side array with size n
*
* fill the left the array from 1 to n - 1 (Inclusive)
* if(input[i] > left [i - 1]) {
* left[i] = input[i];
* } else {
* left[i] = left[i - 1];
* }
*
* fill the right the array from n - 1 to 0 (Inclusive)
* if(input[i] > right[i + 1]) {
* right[i] = input[i];
* } else {
* right[i] = right[i + 1];
* }
*
* int totalWater = 0;
*
* Traverse the main array and for every element
* totalWater = totalWater + (min(left[i], right[i]) - current element);
*
*
*/
public class TrappingRainWater {

public static int totalWater(int [] arr) {

int [] left = new int[arr.length];
int [] right = new int [arr.length];

//fill left array

left[0] = arr[0];
for(int i = 1; i < arr.length; i++) {
left[i] = Math.max(left[i - 1], arr[i]);
}

right[arr.length - 1] = arr[arr.length - 1];
for(int i = arr.length - 2; i >= 0; i--) {
right[i] = Math.max(right[i + 1], arr[i]);
}

int totalWater = 0;

for(int i = 0; i < arr.length; i++) {
totalWater += Math.min(left[i], right[i]) - arr[i];
}

return totalWater;
}


public static void main(String[] args) {

int [] arr = {3, 1, 0, 5, 2, 2};

System.out.println(totalWater(arr));
}
}
74 changes: 74 additions & 0 deletions src/day1/TwoPeopleMeetEachOther.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package day1;

public class TwoPeopleMeetEachOther {


public static boolean isMeet(int d1, int d2, int v1, int v2) {
if(d1 > d2 && v1 > v2) {
System.out.println("They will never meet");
return false;
}

if (d2 > d1 && v2 > v1) {
System.out.println("They will never meet");
return false;
}


//Assuming d1 will always be ahead in starting

while(d1 >= d2) { //d2 > d1

if(d1 == d2) {

System.out.println(d1);
return true;
}
d1 = d1 + v1;

d2 = d2 + v2;
}

return false;

}


public static boolean isMeetUsingRelativeSpeed(int d1, int d2, int v1, int v2) {

if(d1 > d2 && v1 > v2) {
System.out.println("They will never meet");
return false;
}

if (d2 > d1 && v2 > v1) {
System.out.println("They will never meet");
return false;
}

int D = d1 - d2;
int V = v1 - v2;

if(D % V == 0) {
return true;
}

return false;
}

public static void main(String[] args) {

int d1 = 6;
int d2 = 4;

int v1 = 6;
int v2 = 8;



System.out.println(isMeetUsingRelativeSpeed(d1, d2, v1, v2));



}
}
Loading