You need to make a funnel of red numbers.

That's what I have now

public class Matrix { public static void main(String[] args) { // TODO Auto-generated method stub int [][] twoDim = { { 1,2,3,4,5 }, { 5,4,3,2,1 }, { 6,7,8,9,1 }, { 2,4,3,1,3 }, { 5,4,3,2,1 }, }; int max = twoDim[0][0]; for(int i = 0; i < twoDim.length; i++) { // 1 2 3 4 5 for(int j = 0; j <twoDim[i].length; j++) { // 1 2 3 4 5 if(twoDim[i][j]>max) { max = twoDim[i][j]; } } // end of for J // System.out.println(); } // end of for i System.out.println(max); } } 

I just found the maximum from the array, but I can’t understand how to make a funnel automated so that the index decreases by 1 on both sides

  • one
    What does it mean to make a funnel? Coloring them is necessary or what - Andrew Bystrov
  • No, you need to reduce each j index i index on both sides as in the picture - Serik Sybanov
  • Those. Need to find the maximum number of these red numbers? - Andrew Bystrov

1 answer 1

But it works only on square matrices. The search algorithm is as follows: first we reduce one element from the sides, and as we move the middle, we begin to increase

 int [][] twoDim = { { 1,2,3,4,5 }, { 5,4,3,2,1 }, { 6,7,8,9,1 }, { 2,4,3,1,3 }, { 5,4,3,2,1 } }; int max = 0; int wrap = 0; boolean flag = false; for(int i = 0; i < twoDim.length; i++) { for(int j = wrap; j < twoDim[i].length - wrap; j++) { if(twoDim[i][j] > max) { max = twoDim[i][j]; } } if(flag) { wrap--; } else { wrap++; } if(wrap == twoDim[0].length/2) { flag = true; } }