Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vedantzope9 committed Oct 7, 2023
2 parents d1f4bad + 7b187da commit c60f11c
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ private static void merge(int[]arr,int low,int high,int mid){
```
### LinearSearch in 2D Array

```java
public static int[] LinearSearchIn2DArray(int arr[][], int target) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == target)
return new int[]{i, j};
}
}
return new int[]{-1, -1};
}
```
### LinearSearch in 2D Array

```java
public static int[] LinearSearchIn2DArray(int arr[][], int target) {
for (int i = 0; i < arr.length; i++) {
Expand Down Expand Up @@ -307,7 +320,74 @@ public static int[] BinarySearchIn2Darr(int matrix[][], int target) {

}
```
### BinarySearch in 2D array
```java
public static int[] BinarySearchIn2Darr(int matrix[][], int target) {
int rows = matrix.length - 1;
int cols = matrix[0].length - 1;

if (rows == 1) {
return BinarySearch(matrix, target, 0, 0, cols);
}
int rstart = 0, rend = rows;
int cmid = cols / 2;

//Run the loop till two rows are remaining
while (rstart < rend - 1) //while this is true , we will be having more than two rows
{
int rmid = rstart + (rend - rstart) / 2;

if (matrix[rmid][cmid] > target)
rend = rmid;
else if (matrix[rmid][cmid] < target)
rstart = rmid;
else
return new int[]{rmid, cmid};
}
//Now we have two Rows remaining

//1. Check wheather middle col contains the ans
if (matrix[rstart][cmid] == target)
return new int[]{rstart, cmid};
if (matrix[rend][cmid] == target)
return new int[]{rend, cmid};

//2.Consider 4 parts search in that parts

//search in 1st half
if (target <= matrix[rstart][cmid - 1])
return BinarySearch(matrix, target, rstart, 0, cmid - 1);

//2nd half
if (target >= matrix[rstart][cmid + 1])
return BinarySearch(matrix, target, rstart, cmid + 1, cols);
//3rd half
if (target <= matrix[rend][cmid - 1])
return BinarySearch(matrix, target, rend, 0, cmid - 1);
//4th half
if (target <= matrix[rend][cmid + 1])
return BinarySearch(matrix, target, rend, cmid + 1, cols);


return new int[]{-1, -1};
}

//Search in the specific Row provided between the col start and col end
static int[] BinarySearch(int matrix[][], int target, int row, int cstart, int cend) {
while (cstart <= cend) {
int cmid = cstart + (cend - cstart) / 2;

if (matrix[row][cmid] > target) {
cend = cmid - 1;
} else if (matrix[row][cmid] < target)
cstart = cend + 1;
else
return new int[]{row, cmid};
}
return new int[]{-1, -1};

}
```
### SieveOfEratosthenes

```java
Expand Down Expand Up @@ -406,6 +486,28 @@ public static int[] BinarySearchIn2Darr(int matrix[][], int target) {
}
```

### Find mode of integer array

```java
public static int modeArray(int arr[]) {
int mode = 0, maxcount = 0;

for (int i = 0; i < arr.length; i++) {
int count = 0;

for (int j = 0; j < arr.length; j++) {
if (arr[i] == arr[j])
count++;
}
if (count > maxcount) {
maxcount = count;
mode = arr[i];
}
}
return mode;
}
```

### Find sum of integer array

```java
Expand Down

0 comments on commit c60f11c

Please sign in to comment.