Get Biggest Three Rhombus Sums in a Grid — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

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

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

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

— Written by Saurabh Patil • B.Tech CSE • Software Developer

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/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

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.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.