Browser tab content: # Given a 2D array (matrix) inputMatrix of integers, create a…
- User
Browser tab content: #
Given a 2D array (matrix)
inputMatrixof integers, create a functionspiralCopythat copiesinputMatrix’s values into a 1D array in a spiral order, clockwise. Your function then should return that array. Analyze the time and space complexities of your solution.Example:
input: inputMatrix = [ [1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20] ] output: [1, 2, 3, 4, 5, 10, 15, 20, 19, 18, 17, 16, 11, 6, 7, 8, 9, 14, 13, 12]See the illustration below to understand better what a clockwise spiral order looks like.
[image: Spiral matrix] open
Constraints:
-
[time limit] 5000ms
-
[input] array.array.integer
inputMatrix- 1 ≤ inputMatrix[0].length ≤ 100
- 1 ≤ inputMatrix.length ≤ 100
-
[output] array.integer
Remove distracting text, extract the programming question, provide simple, common, and optimized Java solution for it.
-
- Assistant
Programming Question
Create a function that copies values from a 2D array (matrix) of integers into a 1D array in a spiral order, clockwise. The function should return the resulting 1D array.