-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e8193c
commit 9a46fc1
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import java.util.Scanner; | ||
/* | ||
Name: Saddle Point | ||
Source: PepCoding | ||
Link: https://www.pepcoding.com/resources/online-java-foundation/2d-arrays/saddle-point-official/ojquestion | ||
Statement: You are required to find the saddle point of the matrix which is defined as the value which is smallest | ||
in it's row but largest in it's column. | ||
*/ | ||
public class SaddlePoint { | ||
public static void main(String [] args) | ||
{ | ||
Scanner scanner = new Scanner(System.in); | ||
int n = scanner.nextInt(); | ||
int [][] arr = new int [n][n]; | ||
|
||
for(int i =0; i<n; i++) | ||
{ | ||
for(int j =0; j<n; j++) | ||
{ | ||
arr[i][j]= scanner.nextInt(); | ||
} | ||
} | ||
|
||
int min, max, col; | ||
|
||
for(int i =0; i<arr.length; i++) | ||
{ | ||
min = max = arr[i][0]; | ||
col=0; | ||
for(int j =0; j<arr.length; j++) | ||
{ | ||
if(min>arr[i][j]) | ||
{ | ||
min = arr[i][j]; | ||
col =j; | ||
} | ||
} | ||
|
||
for(int j =0; j<arr.length; j++) | ||
{ | ||
if(max<arr[j][col]) | ||
{ | ||
max = arr[j][col]; | ||
} | ||
} | ||
|
||
if(max == min) | ||
{ | ||
System.out.println(max); | ||
return; | ||
} | ||
|
||
} | ||
|
||
System.out.println("Invalid input"); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import java.util.Scanner; | ||
/* | ||
Name: Search In A Sorted Matrix | ||
Source: PepCoding | ||
Link: https://www.pepcoding.com/resources/online-java-foundation/2d-arrays/search-in-a-sorted-2d-array-official/ojquestion | ||
Statement: You are required to find x in the matrix and print it's location int (row, col) format as discussed in | ||
output format below. | ||
*/ | ||
public class SearchInMatrix { | ||
public static void main(String[] args) { | ||
// write your code here | ||
Scanner scanner = new Scanner(System.in); | ||
int n = scanner.nextInt(); | ||
|
||
int[][] arr = new int[n][n]; | ||
|
||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < n; j++) { | ||
arr[i][j] = scanner.nextInt(); | ||
} | ||
} | ||
|
||
int data = scanner.nextInt(); | ||
int i = 0; | ||
int j = n-1; | ||
while(i<arr.length && j>=0) | ||
{ | ||
if(arr[i][j]==data) | ||
{ | ||
System.out.println(i); | ||
System.out.println(j); | ||
return; | ||
} | ||
else if(data> arr[i][j]) | ||
{ | ||
i++; | ||
} | ||
else | ||
{ | ||
j--; | ||
} | ||
|
||
} | ||
|
||
System.out.println("Not Found"); | ||
|
||
|
||
|
||
} | ||
} |