Definition
An arrowhead matrix is a matrix that has all entries equal to 0, except the ones on the main diagonal, top row and leftmost column. In other words, the matrix should look like this:
* * * * * * * * 0 0 0 0 * 0 * 0 0 0 * 0 0 * 0 0 * 0 0 0 * 0 * 0 0 0 0 *
Where each * is any non-zero entry.
Task
Given a square matrix of non-negative integers, check whether it is arrowhead according to the definition above.
You may not take the size of the matrix as input, unless your language’s equivalent to an array is something like a pointer and a length (like C). It will always be at least 3 x 3.
The shortest code in bytes in each language wins.
Input and Output
You can pick among any of the following formats for receiving input:
- A matrix in the native matrix type (if your language has one)
- A 2D array1 (an array of 1D arrays, each corresponding to one row)
- A 1D array (since the matrix is always square)
- A string (you chose the spacing, but please do not abuse this in any way).
When it comes to providing output, you can either report a truthy / falsy value following the standard decision-problem definition, or choose any two distinct and consistent values.
Moreover, you can take input and give output through any standard method, in any programming language, while taking note that these loopholes are forbidden by default. If want to pick any other format or are unsure about something, please ask in the comments.
1: or your language's equivalent (list, vector, etc.)
Examples
Let's look at the following examples:
1 2 2 2 2 1 0 0 3 0 1 0 4 0 0 1
This is an arrowhead matrix (your programs should report a truthy value), because the elements on the main diagonal are 1 1 1 1
, those on the top row are 1 2 2 2
and those on the leftmost column are 1 2 3 4
. All other entries are 0, so this satisfies all the conditions.
3 5 6 7 1 0 8 0 0
This matrix is not arrowhead because there is a 0 on the main diagonal.
9 9 9 9 9 9 0 0 9 7 9 0 9 0 0 9
This one is not arrowhead either, because it contains a 7 in place of a 0.
More test cases
Truthy:
[[1, 1, 1], [1, 1, 0], [1, 0, 1]] [[1, 2, 3, 4], [1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]] [[1, 2, 2, 2], [2, 1, 0, 0], [3, 0, 1, 0], [4, 0, 0, 1]] [[34, 11, 35, 5], [56, 567, 0, 0], [58, 0, 679, 0], [40, 0, 0, 7]]
Falsy:
[[3, 5, 6], [7, 1, 0], [8, 0, 0]] [[9, 9, 9, 9], [9, 9, 0, 0], [9, 7, 9, 0], [9, 0, 0, 9]] [[1, 0, 3, 4], [1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]] [[1, 6, 3, 4], [13, 2, 0, 6], [29, 0, 1, 0], [2, 0, 0, 4]]