Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

19. Spiral Matrix

MediumDay 5 of 60Arrays & Hashing

Given an m x n matrix, return all of its elements in spiral order: go right along the top row, down the right column, left along the bottom row, up the left column, and keep spiralling inwards until every element is visited.

Example 1
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
Example 2
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
Constraints
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100
Hints
Frequently asked at: Microsoft, Amazon, Apple, Zoho, Paytm
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min