## 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.

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]]

• Is it possible that the matrix can contain negative numbers Commented Dec 20, 2017 at 19:14
• @Zacharý No you may assume they are all non-negative. Commented Dec 20, 2017 at 19:15
• Pedant: A two dimensional array and a matrix are not the same thing, nor is it the same as an array of arrays. Is input as a two dimensional array acceptable if your language of choice is civilised enough to support multidimensional arrays? Commented Dec 20, 2017 at 20:07
• @IanBush Yes, a 2D array is totally fine. Commented Dec 20, 2017 at 20:09
• @Mr.Xcoder This would be a sufficiently different and interesting challenge if the arrowhead could point in any direction Commented Dec 21, 2017 at 1:27

# Japt, 16 bytes

Ëe@!X^!(E*Y*nE
e


Test it online!

Man, this takes me back to the good old days when Japt was regularly much longer than other golfing langs...

# K (oK), 27 30 bytes

Solution:

x~a*x|a:a|+(a:1,(#1_x)#0)|=#x:


Try it online!

Explanation:

I must be doing something dumb as the APL solutions are less than half the byte count...

24 bytes spent creating the arrowhead. or together the following three matrices:

/ assume 4x4 matrix
=#x
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

+(a:1,(#1_x)#0)
1 0 0 0
1 0 0 0
1 0 0 0
1 0 0 0

a
1 1 1 1
0 0 0 0
0 0 0 0
0 0 0 0


Full breakdown:

x~a*x|a:a|+(a:1,(#1_x)#0)|=#x: / the solution
x: / save input as x
#   / count length
=    / identity matrix
|     / or with
(            )      / do this together
#0       / take from 0
( 1_x)         / drop first of x
#             / count
1,               / prepend 1
a:                 / save as a
+                    / flip rows/cols
|                     / or with
a                      / a
a:                       / save as a
|                         / or with
x                          / x
x~                             / matches input?


# 05AB1E, 23 bytes

ćsøćsε¾èˆ)¼}˜OŠ¯)ćs˜ĀP‹


Try it online!

This was initially a comment on Kevin's answer, but they told me I should post it separately, and so I did. I should be able to shave off a couple of bytes shortly.

# R, 52 bytes

function(m)all(rbind(1,cbind(1,diag(nrow(m)-1)))-!m)


Try it online!

One-liner doing the same as the other R solution by Giuseppe for the same byte count. Giuseppe managed to save one byte before I even posted this!

# JavaScript ES6, 9785 82 bytes

x=>x.map((_,i)=>p(0,i)+p(i,0)+p(i,i),p=(i,j)=>x[i][j]=+!x[i][j])&&!/[1-9]/.test(x)


F =
x=>x.map((_,i)=>p(0,i)+p(i,0)+p(i,i),p=(i,j)=>x[i][j]=+!x[i][j])&&!/[1-9]/.test(x)

console.log(F([[1,2,3,4,5,6],[9,5,0,0,0,0],[4,0,3,0,0,0],[4,0,0,3,0,0],[4,0,0,0,3,0],[4,0,0,0,0,3]]))

console.log(F([[1,2,3,4,5,6],[9,5,0,0,0,0],[4,0,3,0,0,0],[4,0,7,3,0,0],[4,0,0,0,3,0],[4,0,0,0,0,3]]))

console.log(F([[1,2,0,4,5,6],[9,5,0,0,0,0],[4,0,3,0,0,0],[4,0,0,3,0,0],[4,0,0,0,3,0],[4,0,0,0,0,3]]))

• @Mr. Xcoder For C the equ of an array is a pointer and a length. For JavaScript I modified so it doesn't take the length
– l4m2
Commented Dec 20, 2017 at 20:02

# C# (.NET Core), 73 + 18 = 91 bytes

using System.Linq;
a=>a.SelectMany((b,i)=>b.Select((n,j)=>i<1||j<1||i==j?n>0:n<1)).All(v=>v)


Try it online!

The input is an array of integer-arrays (int[][]) the output is bool.

Ungolfed full program:

using System.Linq;
class Program
{
static void Main(string[] args)
{
System.Func<int[][], bool> f =
a => a.SelectMany(                          //Iterates through all elements of the outer array, applies the following function and flattens the result
(b, i) =>                               //b is the inner array, i is it's index
b.Select(                           //Iterates through all elements of the current array (b) and applies the following function
(n, j) =>                       //n is the integer, j is it's index in the inner array (b)
i < 1 || j < 1 || i == j ?  //If the current element is in the first row, first column or the diagonal
n > 0 :                     //Check if the element is not 0
n < 1))                     //else check if the element is 0
.All(v => v);                           //Check if the value of all elements of the flattened result is true, the result is returned implicitly

//Truthy test cases
System.Console.WriteLine(f(new int[][] { new[] { 1, 1, 1 }, new[] { 1, 1, 0 }, new[] { 1, 0, 1 } }));
System.Console.WriteLine(f(new int[][] { new[] { 1, 2, 3, 4 }, new[] { 1, 1, 0, 0 }, new[] { 1, 0, 1, 0 }, new[] { 1, 0, 0, 1 } }));
System.Console.WriteLine(f(new int[][] { new[] { 1, 2, 2, 2 }, new[] { 2, 1, 0, 0 }, new[] { 3, 0, 1, 0 }, new[] { 4, 0, 0, 1 } }));
System.Console.WriteLine(f(new int[][] { new[] { 34, 11, 35, 5 }, new[] { 56, 567, 0, 0 }, new[] { 58, 0, 679, 0 }, new[] { 40, 0, 0, 7 } }));

//Falsey test cases
System.Console.WriteLine(f(new int[][] { new[] { 3, 5, 6 }, new[] { 7, 1, 0 }, new[] { 8, 0, 0 } }));
System.Console.WriteLine(f(new int[][] { new[] { 9, 9, 9, 9 }, new[] { 9, 9, 0, 0 }, new[] { 9, 7, 9, 0 }, new[] { 9, 0, 0, 9 } }));
System.Console.WriteLine(f(new int[][] { new[] { 1, 0, 3, 4 }, new[] { 1, 1, 0, 0 }, new[] { 1, 0, 1, 0 }, new[] { 1, 0, 0, 1 } }));
System.Console.WriteLine(f(new int[][] { new[] { 1, 6, 3, 4 }, new[] { 13, 2, 0, 6 }, new[] { 29, 0, 1, 0 }, new[] { 2, 0, 0, 4 } }));
}
}

• Suggest i<1|j<1|i==j instead of i<1||j<1||i==j Commented Aug 31, 2020 at 22:49

# PHP, 80 bytes

foreach($_GET[a]as$i=>$r)foreach($r as$k=>$v)($i&&$k&&$i!=$k)==$v&&$b=1;echo!\$b;


# APL NARS, 48 bytes, 24 chars

{(×⍵)≡1⍪1,(⍳∘.=⍳)¯1+↑⍴⍵}


test:

   y←{(×⍵)≡1⍪1,(⍳∘.=⍳)¯1+↑⍴⍵}
a1 a2 a3
1 2 2 2   3 5 6   9 9 9 9
2 1 0 0   7 1 0   9 9 0 0
3 0 1 0   8 0 0   9 7 9 0
4 0 0 1           9 0 0 9
y¨a1 a2 a3
1 0 0


# Java 8, 95 bytes

A lambda from int[][] to int (0 is false, 1 is true).

m->{int i=0,d=m.length,r,c,f=1;while(i<d*d)f=m[r=i/d][c=i++%d]<1==(r*c*(r-c)!=0)?f:0;return f;}


Try It Online

## Ungolfed

m -> {
int
i = 0,
d = m.length,
r, c,
f = 1
;
while (i < d * d)
f =
m[r = i/d][c = i++%d] < 1
== (r * c * (r-c) != 0) ?
f
: 0
;
return f;
}


# Ruby, 65 bytes

->a{(r=0...a.size).all?{|i|r.all?{|j|(a[i][j]>0)^(i!=j&&i*j>0)}}}


Try it online!

# Attache, 33 bytes

{DrawMatrix["\\<:^>",#_]==Clip!_}


Try it online!

DrawMatrix takes a string and a matrix size and "draws" it. In this case, we generate a base arrowhead matrix, like so:

\<:^>
\      draw a line from the top left to the bottom right
<:    move to the left
^   draw a line up to the top
>  draw a line right to the left


Then, we check for equality with the input matrix, replacing nonzeroes with 1.