-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeakFinder2D.java
More file actions
70 lines (63 loc) · 1.62 KB
/
Copy pathpeakFinder2D.java
File metadata and controls
70 lines (63 loc) · 1.62 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*Problem Statement : Find a 2D peak in a given matrix.
* matrix(i,j) is 2D peak iff {matrix(i-1,j),matrix(i+1,j),matrix(i,j+1),matrix(i,j-1)} <= matrix(i,j)
* if matrix(i,j) is in the edge then the adjacent place numbers should be less than matrix(i,j)
* Example : matrix = 1 2 3
* 4 6 5
* Peak = 6*/
import java.util.Scanner;
public class peakFinder2D {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row, col;
row = sc.nextInt();
col = sc.nextInt();
int matrix[][] = new int[row][col];
for( int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
matrix[i][j]=sc.nextInt();
}
}
int midCol= col/2;
int big = matrix[0][midCol];
int rowi = 0;
int peak=-1;
/*finding the global max in the mid column*/
for(int j=1;j<row;j++)
{
if(matrix[j][midCol] > big)
{
big = matrix[j][midCol];
rowi = j;
}
}
peak = peakFinder1d(rowi,midCol,matrix,col);
if(peak != -1)
System.out.println(peak);
else
System.out.println("doesn't exist");
sc.close();
}
/*method to find the peak across the row similar to finding 1D peak*/
public static int peakFinder1d(int i,int j,int matrix[][],int last)
{
int peak = -1;
while (j != last-1 && j != 0){
if(matrix[i][j] < matrix[i][j-1])
{
j = j-1;
}
else if(matrix[i][j] < matrix [i][j+1] )
{
j = j+1;
}
else
{
peak = matrix[i][j];
}
}
peak = matrix[i][j];
return peak;
}
}