Reverse Coding

Easy
0/40
Average time to solve is 20m
profile
Contributed by
25 upvotes
Asked in companies
ZSJio Platforms Limited

Problem statement

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?

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
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.
Constraints:
1 <= T <= 50
1 <= N <= 400
1 <= M <= 400
0 <= MAT[i][j] <= 100

Time Limit: 1 sec
Sample Input 1:
2
2 2
1 2
3 4

2 3
4 6 8
3 7 9
Sample Output 1:
2 1
4 3

8 6 4
9 7 3
Explanation for Sample Input 1:
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
Sample Output 2:
1 2 3
4 5 6
7 8 9

9 1
Explanation for Sample Input 2:
 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].
Hint

Will it be feasible to swap the values for each row?

Approaches (1)
Brute Force

The idea is to perform a swapping operation on each row using the two-pointer technique.

 

The steps are as follows:

  • Initialize two variables ‘start’ and ‘end’ which denotes the start and end indices respectively for the current row.
  • We will iterate from i = 0 to i = N - 1. For each row in the given matrix, we will perform the following operations:
    • We will initialize ‘start’ to 0 and ‘end’ to M-1.
    • We will execute a while loop with the conditionstart index is less than ending index
      • In every iteration, we will swap the value at these indexes.
      • Then we will increment the start and decrement the end.
  • We will return the 2D matrix after the operations are performed as the final answer.
Time Complexity

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). 

Space Complexity

O(1), no extra space required.

 

As we are not using any extra space. Hence, the overall space complexity is O(1). 

Code Solution
(100% EXP penalty)
Reverse Coding
Full screen
Console