1

I am getting output as 91. If I alter for(int j=0;j<=arr.length;j++) then I am getting output as 92. But the actual answer is 93.

package basics;

public class maximumNumber {
    public static void main(String[] args) {
        int arr[][] = {‌{11,21,31,32,33},{41,51,61,62,63},{71,81,91,92,93}};
        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[i][j];
                }
            }
        }

        System.out.println(max);
    }
}
Leo
  • 1,829
  • 4
  • 27
  • 51

5 Answers5

6

The problem is here:

for(int j=0;j<arr.length;j++)

You should actually be looping over the length of the inner array, not of the outer array. This should actually be

for(int j=0;j<arr[i].length;j++)

Edit: A key piece of information here is that you have an array of arrays. Your "outer" array has 3 items, each of which is itself an array of integers. To help understand this, try running the following code:

public class maximumNumber {
    public static void main(String[] args) {
        int arr[][] = {{11,21,31,32,33},{41,51,61,62,63},{71,81,91,92,93}};
        int max = arr[0][0];

        for(int i=0;i<arr.length;i++) {
            int[] innerArray = arr[i];

            System.out.println("------------------ Begin array " + i + " -----------");

            for(int j=0; j< innerArray.length; j++) {
                System.out.println(innerArray[j]);

                if(arr[i][j]>max) {
                    max=arr[i][j];
                }
            }

            System.out.println("------- End of array " + i + " --------");
        }

        System.out.println(max);
    }
}

Here's the output:

------------------ Begin array 0 -----------
11
21
31
32
33
------- End of array 0--------
------------------ Begin array 1 -----------
41
51
61
62
63
------- End of array 1--------
------------------ Begin array 2 -----------
71
81
91
92
93
------- End of array 2 --------
93
5

In second loop u need to use arr[i].length

  int arr[][] = {{11,21,31,32,33},{41,51,61,62,63},{71,81,91,92,93}};

  int max = arr[0][0];

   for(int i=0;i<arr.length;i++){
          for(int j=0;j<arr[i].length;j++){
                if(arr[i][j]>=max){max=arr[i][j];}
           }
     }
Dumidu Udayanga
  • 864
  • 2
  • 9
  • 21
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
5

Your condition j<arr.length is wrong. arr.length is 3, but you need j to continue until 5, the length of your subarrays.

Should be j < arr[i].length.

khelwood
  • 55,782
  • 14
  • 81
  • 108
1

In the second loop use for (int j = 0; j < arr[i].length; j++) {, this will give you full element loop.

  • @ParthSane sure: this way we are taking into account all the values of each subarray – Duricic Milos Apr 09 '20 at 22:41
  • @EJoshuaS-ReinstateMonica you are right. Now see the first answer is very descriptive. I think I have tried vote up but only thing I can do with current rank is give answers and comment – Duricic Milos Apr 09 '20 at 22:45
0

Starting with Java 8, you can use streams to do this.

e.g. intStream.max(Integer::compare)

But you will need to flatten the data first.

import java.util.Arrays;
import java.util.stream.Stream;

public class Flatten {
    public static void main(String[] args) {
        int arr[][] = {
            { 11, 21, 31, 32, 33 },
            { 41, 51, 61, 62, 63 },
            { 71, 81, 91, 92, 93 }
        };
        System.out.printf("Max: %d", max(arr));
    }

    private static int max(int[][] matrix) {
        return flatten(matrix).max(Integer::compare).get().intValue();
    }

    private static Stream<Integer> flatten(int[][] matrix) {
        return Arrays.asList(matrix).stream()
            .map(row ->  Arrays.stream(row)).flatMap(a -> a.boxed());
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132