40
2
This is a 2-dimensional version of this question.
Given a non-empty 2-dimensional array/matrix containing only non-negative integers:
$$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & 0 & 1 & 0 \\ {\color{Red}0} & {\color{Red}0} & 0 & 0 & 1 \\ {\color{Red}0} & {\color{Red}0} & 1 & 1 & 1 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} $$
Output the array with surrounding zeroes removed, i.e. the largest contiguous subarray without surrounding zeroes:
$$ \begin{bmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 1 & 1 \end{bmatrix} $$
Examples:
$$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & 0 & 1 & 0 \\ {\color{Red}0} & {\color{Red}0} & 0 & 0 & 1 \\ {\color{Red}0} & {\color{Red}0} & 1 & 1 & 1 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 1 & 1 \end{bmatrix} $$
Input:
[[0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 0, 1, 1, 1], [0, 0, 0, 0, 0]]
Output:
[[0, 1, 0], [0, 0, 1], [1, 1, 1]]
$$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & 0 & 0 & 3 \\ {\color{Red}0} & 0 & 0 & 0 \\ {\color{Red}0} & 5 & 0 & 0 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 0 & 0 & 3 \\ 0 & 0 & 0 \\ 5 & 0 & 0 \end{bmatrix} $$
Input:
[[0, 0, 0, 0], [0, 0, 0, 3], [0, 0, 0, 0], [0, 5, 0, 0], [0, 0, 0, 0]]
Output:
[[0, 0, 3], [0, 0, 0], [5, 0, 0]]
$$ \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \mapsto \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} $$
Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} \end{bmatrix} $$
Input:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Output:
[]
$$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ 1 & 1 & 1 & 1 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 1 & 1 & 1 & 1 \end{bmatrix} $$
Input:
[[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0]]
Output:
[[1, 1, 1, 1]]
$$ \begin{bmatrix} {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} $$
Input:
[[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]]
Output:
[[1], [1], [1]]
$$ \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 1 \\ 1 & 1 & 1 & 1 \end{bmatrix} \mapsto \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 1 \\ 1 & 1 & 1 & 1 \end{bmatrix} $$
Input:
[[1, 1, 1, 1], [1, 2, 3, 1], [1, 1, 1, 1]]
Output:
[[1, 1, 1, 1], [1, 2, 3, 1], [1, 1, 1, 1]]
This is very difficult in JavaScript. I'm looking forward to a concise answer in that language. – MattH – 2018-07-27T15:40:29.633
3@MattH Nothing is difficult in a non-esoteric language.
:)
Just difficult to make it short. – user202729 – 2018-07-27T16:20:05.9201Can we give a falsey output instead of an empty matrix, for the last test case? – sundar - Reinstate Monica – 2018-07-27T18:21:34.160
1Also, if output can be a non-square matrix, please add a test case for that. – sundar - Reinstate Monica – 2018-07-27T18:44:52.277
1A test case that broke my earlier submission:
[[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0]]
(the result having a width/height of1
) – Conor O'Brien – 2018-07-27T20:24:22.077@sundar What does that falsey value look like? If it is just
false
, I think it is OK. – alephalpha – 2018-07-28T04:04:45.5501Hey, is it possible to add the test case $$\begin{bmatrix}1&1&1&1\1&2&3&1\1&1&1&1\end{bmatrix}$$ – Beta Decay – 2018-07-28T10:35:23.440
1I would suggest adding a test case like this as well:
$$ \begin{bmatrix} {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \ {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \ {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \end{bmatrix} $$ – Night2 – 2018-07-28T12:00:16.827