Get Biggest Three Rhombus Sums in a Grid - Problem

You are given an m x n integer matrix grid.

A rhombus sum is the sum of the elements that form the border of a regular rhombus shape in grid. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell.

A rhombus can have an area of 0 (single cell), which means it consists of just one cell. The rhombus border includes all cells that form the diamond shape outline.

Return the biggest three distinct rhombus sums in the grid in descending order. If there are less than three distinct values, return all of them.

Input & Output

Example 1 — Basic Grid with Multiple Rhombuses
$ Input: grid = [[3,4,5,1,3],[3,3,4,2,3],[20,30,200,40,10],[1,5,5,4,1],[4,3,2,2,5]]
Output: [228,216,211]
💡 Note: The largest rhombus sum is 228 (border of large diamond), followed by 216 and 211. We return the three biggest distinct values in descending order.
Example 2 — Small Grid
$ Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [20,16,13]
💡 Note: In a 3x3 grid, we can form various rhombuses. The largest sum is 20 from the border 2+6+8+4, then 16 and 13 from other diamond patterns.
Example 3 — Single Row
$ Input: grid = [[7,7,7]]
Output: [7]
💡 Note: With only single cells possible, all rhombuses have the same sum of 7. We return only one distinct value.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 50
  • 1 ≤ grid[i][j] ≤ 105

Visualization

Tap to expand
Get Biggest Three Rhombus Sums in a Grid INPUT 3 4 5 1 3 3 3 4 2 3 20 30 200 40 10 1 5 5 4 1 4 3 2 2 5 Rhombus Shape: 5x5 integer matrix Find top 3 rhombus sums grid[m][n], m=5, n=5 ALGORITHM STEPS 1 Initialize Set Use Set to store distinct rhombus sums found 2 Iterate Centers For each cell (i,j) as potential rhombus center 3 Expand Sizes Try all valid rhombus sizes size=0 (single cell) to max 4 Calculate Sum Sum border elements of rhombus, add to Set Distinct Sums Set {228, 216, 211, 209, 200, 72, 63, 61, 40, 30, ...} Sort desc, return top 3 Early exit when found max possible sums FINAL RESULT Largest Rhombus (228): 3 4 5 1 3 3 3 4 2 3 20 30 200 40 10 3+5+3+4+200+2+4+5+2 = 228 Top 3 Sums 1st: 228 (size 2 rhombus) 2nd: 216 (size 1 rhombus) 3rd: 211 (size 1 rhombus) OUTPUT [228, 216, 211] OK - Descending order All distinct values Key Insight: Using a Set automatically handles duplicates, ensuring we only track distinct rhombus sums. For each center (i,j), we expand rhombus size from 0 (single cell) until boundaries are exceeded. Early termination: once we have enough distinct values, we can stop searching larger rhombuses. TutorialsPoint - Get Biggest Three Rhombus Sums in a Grid | Optimized with Set and Early Termination
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
23.0K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen