As a part of its annual techno-cultural fest, NinjaCity will conduct a technical event, Decode, where it has given a matrix, and the participants have to decode it.
The participants are given a N*M matrix; they need to print the rows in reverse order. Do this for every row.
Among the participants, a participant named Ninja is new to programming and doesn’t have much experience; he asks you to solve the problem. Can you help Ninja reverse all the rows in reverse order?
The first line of input contains an integer ‘T,’ denoting the number of test cases. The test cases follow.
The first line of each test case contains two numbers ‘N’ and ‘M’, denoting the number of rows and columns in the matrix.
The following ‘N’ lines of each test case contain ‘M’ integers of every row (separated by space).
Output Format:
For each test case, print 'N' lines with each line consisting of 'M' integers. (row elements separated by space)
Print the output of each test case in a separate line.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= N <= 400
1 <= M <= 400
0 <= MAT[i][j] <= 100
Time Limit: 1 sec
2
2 2
1 2
3 4
2 3
4 6 8
3 7 9
2 1
4 3
8 6 4
9 7 3
In the first test case, the first row is [1,2], which on reversing becomes [2,1]. Similarly, the second row is [3.4], which on reversing becomes [4,3].
In the second test case, the first row is [4,6,8], which on reversing becomes [8,6,4]. Similarly, the second row is [3,7,9], which on reversing becomes [9,7,3].
Sample Input 2:
2
3 3
3 2 1
6 5 4
9 8 7
1 2
1 9
1 2 3
4 5 6
7 8 9
9 1
In the first test case, the first row is [3,2,1], which on reversing becomes [1,2,3]. The second row is [6,5,4], which on reversing becomes [4,5,6]. The final row is [9,8,7] which on reversing becomes [7,8,9];
In the second test case, the only row is [1,9] which on reversing becomes [9,1].
Will it be feasible to swap the values for each row?
The idea is to perform a swapping operation on each row using the two-pointer technique.
The steps are as follows:
O(N*M), where N and M are the rows and columns of the matrix, which reverses, respectively.
We are traversing each row of the matrix that takes O(N) time complexity and inside each loop, we are using the two-pointer technique to swap the elements between the columns, which takes additional time (M). Hence the overall time complexity is O(N*M).
O(1), no extra space required.
As we are not using any extra space. Hence, the overall space complexity is O(1).