We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I know that the order is higher than computing hourglass sum without for loops . I just wanted to say there is another way.
int maxSum = Integer.MIN_VALUE ;
//shift a 3*3 window through the 2D-array and calculate hourGlassSum of it
for(int i=0; i < 4; i++){
for(int j=0; j < 4; j++){
maxSum = Math.max(hourGlassSum(arr,i,j),maxSum);
}
}
System.out.println(maxSum);
}
public static int hourGlassSum(int[][] arr,int x,int y){
int i , j ,hourGlass = 0;
//compute sum of all items in the window
for (i = x; i < x + 3; i++) {
for (j = y; j < y + 3; j++) {
hourGlass += arr[i][j];
}
}
//exclude numbers which are not in the hour glass but has been added in last step
hourGlass -= arr[x+1][y];
hourGlass -= arr[x + 1][y + 2];
return hourGlass;
}
Day 11: 2D Arrays
You are viewing a single comment's thread. Return to all comments →
I know that the order is higher than computing hourglass sum without for loops . I just wanted to say there is another way.