Write a code to convert a matrix in specific way without using extra space.
Input: 1 2 3 4 5 6 7 8 9 Output: 1 6 7 2 5 8 3 4 9
At first look, the problem seems similar to finding transpose of the matrix. But if you look carefully, you will notice that every even column in output matrix has elements of corresponding row in input matrix in opposite order.
We strongly recommend you to minimize your browser and try this yourself first.
The problem can be easily converted to transpose of the matrix by doing some modification to the input matrix. If we invert every even row present in input matrix, we can use solution given here to convert the matrix in desired order and that too without using any auxiliary memory.
Below is C++ implementation of the idea.
Given Matrix: 1 2 3 4 5 6 7 8 9 10 11 12 Transformed Matrix: 1 8 9 2 7 10 3 6 11 4 5 12
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Inplace rotate square matrix by 90 degrees | Set 1
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Find a specific pair in Matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Program to convert given Matrix to a Diagonal Matrix
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- C++ program to Convert a Matrix to Sparse Matrix
- Convert given Matrix into sorted Spiral Matrix
- Print all elements in sorted order from row and column wise sorted matrix
- Sum of both diagonals of a spiral odd-order square matrix
- Sort a Matrix in all way increasing order
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Search in a sorted 2D matrix (Stored in row major order)
- Print Lower Hessenberg matrix of order N
- Print Upper Hessenberg matrix of order N