Last Updated: 27 Nov, 2020

Reverse Coding

Easy
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?

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

Approaches

01 Approach

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.