Skip to content

Commit 805ec51

Browse files
committed
Solution to search a Maximum Number in a 2D Array
1 parent d8b6fbf commit 805ec51

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

‎src/LinearSearch/SearchIn2DArray.java‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,29 @@ public static void main(String[] args) {
88
{34, 5, 6},
99
{56, 7, 8},
1010
{45, 67, 90},
11-
{18,12}
11+
{18,1234}
1212
};
1313
int target = 12;
1414

1515
int[] answer = searchArray(array,target);
1616

17-
System.out.println(Arrays.toString(answer));
17+
// System.out.println(Arrays.toString(answer));
18+
System.out.println(searchArrayForMaximum(array));
1819

1920
}
2021

22+
static int searchArrayForMaximum(int[][] array){
23+
int max = Integer.MIN_VALUE;
24+
for (int row = 0; row < array.length; row++){
25+
for (int col = 0; col < array[row].length; col++){
26+
if (array[row][col] > max){
27+
max = array[row][col];
28+
}
29+
}
30+
}
31+
return max;
32+
}
33+
2134
static int[] searchArray(int[][] array, int target){
2235
for (int row = 0; row < array.length; row++){
2336
for (int col = 0; col < array[row].length; col++){

0 commit comments

Comments
 (0)