Last Updated: 30 Nov, 2020

# Print Matrix

Easy

## Problem statement

#### The Xs and Os must be filled. Alternatively, the matrix should have the outermost rectangle of Xs, then a rectangle of Os, then a rectangle of Xs, and so on.

##### For Example:
``````N = 3 and M = 3

Required Matrix-

XXX
XOX
XXX
``````
##### Input Format:
``````The first line contains a single integer ‘T’ denoting the number of test cases to be run. Then the test cases follow.

The first line of each test case contains two integers ‘N’ and ‘M’ denoting the number of rows and columns in the required matrix.
``````
##### Output Format:
``````For each test case print 'N' strings denoting 2D matrix satisfying the above conditions.
``````
##### Note
``````You are not required to print anything; it has already been taken care of. Just implement the function and return the matrix.
``````
##### Constraints:
``````1 <= T <= 50
1 <= N <= 100
1 <= M <= 100

Time Limit: 1 sec.
``````

## Approaches

### 01 Approach

We can see that for, given ‘N’ and ‘M’, we have exactly (min('N', ‘M’) + 1) / 2 rectangles to fill. So, we can iterate through each and every rectangle starting from the outermost one and fill it with the required character.

#### Algorithm-

1. Declare empty matrix of size ‘N’  * ‘M’
2. Find the value of (min( 'N' , ‘M’ ) + 1) / 2 and store it in integer ‘T’.
3. Run a loop on ‘T’ from 0 to ‘T’ - 1.
• initialize two variables ‘X’ and ‘Y’, with the value ‘T’.
• Using a while loop increment ‘X’ till ‘N’ - ‘T’ with constant ‘Y’ and for each position update the value at that position. If ‘T’ is even, then the value will be ‘O’ else ‘X’.
• Now, Increment Y with the same procedure till ‘M’ - ‘T’  and update the value at that position and again after 'Y' is ‘N’ - ‘T’ decrement ‘X’ till ‘T’ and update the value at each position.
• At last, decrement the ‘Y’ till ‘T’ and update the value of each position.
4. Return the matrix.