Skip to main content
3 of 3
fixed explanation
Giuseppe
  • 28.8k
  • 3
  • 31
  • 105

MATL, 17 15 bytes

-2 bytes thanks to Luis Mendo!

0i:oEqG:EqY"Ysh

Try it online!

Explanation for n=3:

0		% push 0
 i:		% read input as integer, push range
		% stack: [0, [1 2 3]]
   o		% modulo 2, stack: [0, [1 0 1]]
    Eq		% double and decrement, stack: [0, [1 -1 1]]
      G:	% push input and range again
		% stack: [0, [1 -1 1], [1 2 3]]
        Eq	% double and decrement,
		% stack: [0, [1 -1 1], [1 3 5]]
	  Y"	% run-length decoding
		% stack: [0, [1 -1 -1 -1 1 1 1 1 1]]
	    Ys	% cumulative sum
		% stack: [0, [1  0 -1 -2 -1  0  1  2  3]]
	      h	% horizontally concatenate
		% end of program, automatically print the stack
Giuseppe
  • 28.8k
  • 3
  • 31
  • 105