Last Updated: 4 Jan, 2022

Naughty Luke

Easy
Asked in companies
OracleAmazon

Problem statement

Luke, a naughty kid, went to a museum one day. He sees an ancient grid 'A' consisting of 'N' rows and 'N' columns. Each cell of a grid is coloured with either black or white.

Luke performs two sequential operations on the grid ‘A’. First, he twists the grid vertically around the center. After that, as the second operation, he changes the colours of all the cells. If the cell is white, it is changed to black and vice-versa.

The following drawings show the twist operation and the colour change operation, respectively :

The black colour is represented by '1' and white colour is represented by '0' here. You have to print the final grid.

Example :
'N' = 2, 'A' = { {0, 1}, {0, 0} }.

After twisting, the grid looks like :
'A' =  { {1, 0}, {0, 0} }.

After changing the colours, the grid looks like :
'A' =  { {0, 1}, {1, 1} }.
Input Format :
The first line contains an integer 'T', which denotes the number of test cases to be run. Then the test cases follow.

The first line of each test case contains an integer 'N' denoting the dimension of the grid.

The next 'N' lines contain 'N' space-separated integers denoting the colour of a cell.
Output format :
For each test case, print 'N' lines containing 'N' space-separated integers denoting the colour of each cell of the final grid.

Print the output of each test case in a new line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 100.

Sum of 'N' ^ 2 over all the test cases is <= 10 ^ 4.

Time Limit: 1 sec

Approaches

01 Approach

 

Approach :

 

  • We will swap the first half of a row with the second half and change the colours of all the cells in the row.
  • We will do this for each row one by one.


 

Algorithm:

 

  • Loop through all the rows from 'i' = 0 to 'i' = 'N' - 1 :
    • Loop through the cells in this from 'j' = 0 to 'j' = ceil ( 'N' / 2 ) - 1 :
      • Swap( 'A[ i ][ j ]', 'A[ i ][ N - j  - 1]' ).
      • Change the colour of 'A[ i ][ j ]'.
      • If ( 'j' is not equal to 'N' - 'j' - 1 ) :
        • Change the colour of 'A[ i ][ N - j - 1]'.
  • Return 'A'.