Java array example for interview questions
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
Java array highest element in upper triangular matrix
class HighestElementInUpperHalf{ public static void main(String... z){ int b=max(new int[][] {{1,2,3},{4000,50,6},{7,8,9}}); System.out.println("Highest number in this array="+b); } static int max(int d[][]){ int c=d[0][0]; for(int i=0;i<d.length;i++) { for(int j=(i+1)-1;j<d[i].length;j++){ if(c<d[i][j]){ c=d[i][j]; } } } return c; } }
Output
Highest number in this array=50