Track & Visualize

Get Biggest Three Rhombus Sums in a Grid

Choose Your Learning Path

How would you like to learn today? Visualize algorithms in real time, explore them step by step through reading, or challenge yourself with a test.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations. See exactly how the code executes in real time.

๐Ÿ“– Passive Learning

Read comprehensive problem explanation, reference links, and explore the code at your own pace.

๐ŸŽฏ Mandatory Challenge

Drag and arrange the algorithm steps in the correct execution order.

Understanding Get Biggest Three Rhombus Sums in a Grid
Detailed explanation and reference materials
Problem Overview

ยท Get Biggest Three Rhombus Sums in a Grid

Difficulty: Medium ย |ย  Topics: Matrix, Prefix Sum, Enumeration


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ยฐ with each of its corners centered in a grid cell.

Note: A rhombus can have an area of 0, represented by a single cell.

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


Examples

Example 1

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]

dfdsf

  • ๐Ÿ”ต Blue: 20 + 3 + 200 + 5 = 228
  • ๐Ÿ”ด Red: 200 + 2 + 10 + 4 = 216
  • ๐ŸŸข Green: 5 + 200 + 4 + 2 = 211

Example 2

Input:  grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [20, 9, 8]
  • ๐Ÿ”ต Blue: 4 + 2 + 6 + 8 = 20
  • ๐Ÿ”ด Red: 9 (area 0 rhombus โ€” bottom right)
  • ๐ŸŸข Green: 8 (area 0 rhombus โ€” bottom middle)

Example 3

Input:  grid = [[7,7,7]]
Output: [7]

All three possible rhombus sums are identical โ€” return [7].


Constraints

ParameterBound
m == grid.lengthโ€”
n == grid[i].lengthโ€”
m, n1 โ‰ค m, n โ‰ค 50
grid[i][j]1 โ‰ค value โ‰ค 10โต

Java Solution

java
class Solution {
    public int[] getBiggestThree(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        TreeSet<Integer> set = new TreeSet<>();

        // iterate every center cell (r, c) and every radius k
        for (int r = 0; r < m; r++) {
            for (int c = 0; c < n; c++) {
                // radius 0 โ€” single cell
                addToSet(set, grid[r][c]);

                for (int k = 1; r - k >= 0 && r + k < m
                         && c - k >= 0 && c + k < n; k++) {
                    int sum = 0;
                    // top โ†’ right (โ†˜)
                    for (int i = 0; i < k; i++)
                        sum += grid[r - k + i][c + i];
                    // right โ†’ bottom (โ†™)
                    for (int i = 0; i < k; i++)
                        sum += grid[r + i][c + k - i];
                    // bottom โ†’ left (โ†–)
                    for (int i = 0; i < k; i++)
                        sum += grid[r + k - i][c - i];
                    // left โ†’ top (โ†—)
                    for (int i = 0; i < k; i++)
                        sum += grid[r - i][c - k + i];
                    addToSet(set, sum);
                }
            }
        }

        int[] res = new int[set.size()];
        int idx = 0;
        for (int val : set.descendingSet())
            res[idx++] = val;
        return res;
    }

    private void addToSet(TreeSet<Integer> set, int val) {
        set.add(val);
        if (set.size() > 3) set.pollFirst(); // keep only top 3
    }
}

Time Complexity: O(m ยท n ยท min(m, n))
Space Complexity: O(1) โ€” TreeSet holds at most 3 elements


Categories
2d-arrays
Prefix Sum
Enumeration
leetcode-problem-of-the-day
java
Reference Link
https://leetcode.com/problems/get-biggest-three-rhombus-sums-in-a-grid/editorial/
Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.

Loading component...

Java
Output:
Understood Algorithm , Test Me now ๐ŸŽฎ
JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time, so you understand not just the result, but the journey.

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time, so you understand not just the result, but the journey.

๐Ÿง  Logic Puzzle
Arrange the Algorithm Correctly ๐Ÿงฉ

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ? ย 

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

๐Ÿ”’ Locked steps cannot be moved. Only unlocked steps are draggable.

๐Ÿ”Š Enable sound for swap feedback and completion effects.

ยฉ 2026 | Privacy Policy | About