Matrix Cells in Distance Order Problem

Matrix Cells in Distance Order Problem — ExecCode Easy DSA Practice

Solve the Matrix Cells in Distance Order problem on ExecCode. Free online easy DSA practice in Arrays - Logic Building. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Description You are given four integers row, cols, rCenter, and cCenter. Your task is to compute the required answer and return the coordinates of all cells in the matrix, sorted by their distance from (rCenter, cCenter) from the smallest distance to the largest distance. There is a rows x cols matrix and you are on the cell with the coordinates (rCenter, cCenter). You may return the answer in any order that satisfies this condition. The distance between two cells (r1, c1) and (r2, c2) is |r1 - r2| + |c1 - c2|.

Examples

Input {"rows": 1, "cols": 2, "rCenter": 0, "cCenter": 0}; Output [[0, 0], [0, 1]]. Input {"rows": 2, "cols": 2, "rCenter": 0, "cCenter": 1}; Output [[0, 1], [0, 0], [1, 1], [1, 0]]

Constraints

1 <= rows, cols <= 100 0 <= rCenter < rows 0 <= cCenter < cols

Practice Matrix Cells in Distance Order free on ExecCode. Browse DSA problems, topic map, and placement guides.