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

20. Rotate Image

MediumDay 6 of 60Arrays & Hashing

You are given an n x n matrix representing an image. Rotate it by 90 degrees clockwise, in place: you must change the given matrix directly and not allocate another matrix.

Example 1
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
The first column read from bottom to top 7,4,1 becomes the first row.
Example 2
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Constraints
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000
Hints
Frequently asked at: Microsoft, Amazon, Apple, Adobe, Samsung
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