-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaddle-point.java
More file actions
39 lines (30 loc) · 1.03 KB
/
saddle-point.java
File metadata and controls
39 lines (30 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* Saddle Point
Time Complexity: O(n^2) & Space Complexity: O(1)
*/
class Solution{
void saddlePoint(int arr[][], int n) {
for(int i=0; i<n; i++){
int minr = arr[i][0], colno = 0;
// finding least value in a row
for(int j=0; j<n; j++){
if( arr[i][j] < minr ){ // if current elem is less than min number
colno = j;
minr = arr[i][j];
}
}
// finding max value in a specific column
boolean saddlePoint = true;
for(int k=0; k<n; k++){
if( arr[k][colno] > minr ){ // if current elem is more than min number
saddlePoint = false;
break;
}
}
if( saddlePoint == true ){
System.out.print( minr );
return;
}
}
System.out.print("Invalid input");
}
}