2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5a23c84252665b21eecc801c
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Spiral matrix
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 302321
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: spiral-matrix
							 
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2020-03-30 11:23:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Produce a spiral array. A *spiral array*  is a square arrangement of the first N< sup > 2</ sup >  natural numbers, where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given **5** , produce this array:
							 
						 
					
						
							
								
									
										
										
										
											2020-03-30 11:23:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< pre >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								0  1  2  3  4
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								15 16 17 18 5
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								14 23 24 19 6
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								13 22 21 20 7
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								12 11 10  9 8
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`spiralArray`  should be a function. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(typeof spiralArray == 'function');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`spiralArray(3)`  should return an array. 
						 
					
						
							
								
									
										
										
										
											2020-03-30 11:23:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(Array.isArray(spiralArray(3)));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`spiralArray(3)`  should return `[[0, 1, 2],[7, 8, 3],[6, 5, 4]]` . 
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(spiralArray(3), [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [0, 1, 2],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [7, 8, 3],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [6, 5, 4]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-03-30 11:23:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`spiralArray(4)`  should return `[[0, 1, 2, 3],[11, 12, 13, 4],[10, 15, 14, 5],[9, 8, 7, 6]]` . 
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(spiralArray(4), [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [0, 1, 2, 3],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [11, 12, 13, 4],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [10, 15, 14, 5],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [9, 8, 7, 6]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								]);
							 
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`spiralArray(5)`  should return `[[0, 1, 2, 3, 4],[15, 16, 17, 18, 5],[14, 23, 24, 19, 6],[13, 22, 21, 20, 7],[12, 11, 10, 9, 8]]` . 
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(spiralArray(5), [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [0, 1, 2, 3, 4],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [15, 16, 17, 18, 5],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [14, 23, 24, 19, 6],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [13, 22, 21, 20, 7],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [12, 11, 10, 9, 8]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-03-30 11:23:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-03-08 18:00:54 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function spiralArray(n) {
							 
						 
					
						
							
								
									
										
										
										
											2020-09-15 09:57:40 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-03-08 18:00:54 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function spiralArray(n) {
							 
						 
					
						
							
								
									
										
										
										
											2020-03-30 11:23:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  var arr = Array(n),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    x = 0,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    y = n,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    total = n * n--,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    dx = 1,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    dy = 0,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    i = 0,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    j = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  while (y) arr[--y] = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  while (i <  total )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    arr[y][x] = i++;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    x += dx;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    y += dy;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (++j == n) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (dy <  0 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        x++;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        y++;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        n -= 2;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      j = dx;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      dx = -dy;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      dy = j;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      j = 0;
							 
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2020-03-30 11:23:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return arr;
							 
						 
					
						
							
								
									
										
										
										
											2018-12-30 19:05:56 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```