Interview Question
Country: United States
O(m*n)
int[][] n;
int sum = 0; Integer max = null; Integer min = null;
for(int i = 0; i < n.length; i++){
for(int j = 0; j < n[j].length; j++){
sum+=n[i][j];
if(max == null || n[i][j] > max) max = n[i][j];
if(min == null || n[i][j] < min) min = n[i][j];
}
}
System.out.println(min);
System.out.println(max);
System.out.println(sum);
full scan algorithm, O(m*n), where m - number of rows, n - number of columns, all the result values are collected in one loop for m*n iterations.
- zr.roman November 27, 2015