+ 1

How to find largest number in multi dimensional array?

Example Input:{{2,3,5},{6,8,4}} Output: 8

25th Mar 2018, 1:47 PM
karthick
3 odpowiedzi
+ 1
You could filter through easy element and find the largest number. For instance: int largestNumber = 0; for(int i=0;i < input.length;i++){ for(int j=0;j < input[i].length;j++){ if(input[i][j] > largestNumber){ largestNumber = input[i][j]; } } }
25th Mar 2018, 1:51 PM
Ariela
Ariela - avatar
+ 1
For the second for loop use arr[i].length And use max = arr[i][j]; (not two js)
25th Mar 2018, 2:00 PM
Ariela
Ariela - avatar
0
public class MinumArray { public static void main(String[] args) { // TODO Auto-generated method stub int[][] arr= {{2,3,4},{1,0,2}}; int max = arr[0][0]; for(int i=0;i<arr.length;i++) { for(int j=0;j<arr.length;j++) if(arr[i][j]>max) { max=arr[j][j]; } } System.out.println(max); } } Some problem in mycode
25th Mar 2018, 1:58 PM
karthick