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.

- Prepare
- Data Structures
- Arrays
- 2D Array - DS
- Discussions

# 2D Array - DS

# 2D Array - DS

+ 83 comments if you want to pass test Case 3 and 5 dont initialize max value to 0.

+ 70 comments Python3 answer

sum = [] for i in range(len(arr)-2): for j in range(len(arr)-2): sum.append(arr[i][j]+arr[i][j+1]+arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]) print(max(sum))

+ 13 comments How the heck is this problem ranked as easy? Am I missing something or do people jus tlook at the solutions after 1 glance at the problem?

+ 15 comments A Javascript solution using

`O(n*m)`

time complexity and`O(1)`

space complexity.function hourGlass(arr) { // we could set this to 3 given the problems constraings, but this allows changes maxX = 3; // + (arr[0].length % 3) maxY = 3; // + (arr.length % 3) total = -Infinity; // has to be -64, but // begin at y == 0 for (let y = 0; y <= maxY; y++) { for (let x = 0; x <= maxX; x++) { // sum the top of hourglass let sum = arr[y][x] + arr[y][x+1] + arr[y][x+2]; // get the middle of hourglass sum += arr[y+1][x+1]; // sum the bottom of hourglass sum += arr[y+2][x] + arr[y+2][x+1] + arr[y+2][x+2] // don't store result to keep space complexity down if (total < sum) total = sum; } } return total; }

+ 14 comments I think where most people are having problems (and where I

*was*having problems) is that the array is being read in and then processed on opposite coordinates ( e.g. read in as (x, y) but then process as (y, x) ).I decided this was the problem for me after I read in the array as a 1D array and then was able to solve no problem.

Load more conversations

Sort 3076 Discussions, By:

Please Login in order to post a comment