Reshape the Matrix

Easy
0/40
Average time to solve is 10m
profile
Contributed by
3 upvotes
Asked in companies
DunzoFlipkart limited

Problem statement

One of your friends started working at a toy store, and today is his first day. He gets a box of some toys. The box looks like a grid 'B' consisting of 'N' rows and 'M' columns, and each cell has exactly one toy. His task is to arrange these toys on the shelf 'S' in front of him, which is empty. The shelf has 'R' rows and 'C' columns. Help your friend to arrange the toys on the shelf 'S' in the same row-order traversal as in the box 'B'.

You are given the box arrangement of toys in a matrix of size 'N * M' where values in the matrix are integers and denote the IDs of toys. Your task is to return a matrix of size 'R * C' denoting the shelf arrangement of toys.

After arranging the toys on the shelf 'S', if any toy remains in the box or any cell remains empty in the shelf, return the given box arrangement.

Example :
N = 2
M = 2
B = [ [1, 2], [3, 4] ]
R = 1
C = 4

Both arrangements (box and shelf) are shown below:

Return [ [1, 2, 3, 4] ] as our final answer.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains an integer 'T', denoting the number of test cases.
Then the test cases follow:

The first line of each test case contains two space-separated integers, 'N' and 'M', denoting the number of rows and columns in the box 'B', respectively.

The following 'N' lines contain 'M' integers, denoting the toys' IDs in box 'B'.

Next line contains two space-separated integers, 'R' and 'C', denoting the number of rows and columns in the shelf 'S', respectively.
Output Format :
For each test case, print a matrix of size 'R*C' denoting the toy's arrangement on the shelf 'S'.

Print the output of each test case in a new 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' <= 10
1 <= 'N', 'M', 'R', 'C' <= 100
-10^3 <= 'B[i][j]' <= 10^3

Time Limit: 1 sec
Sample Input 1 :
2
2 4
5 0 3 2
1 5 5 3
4 2
3 3
3 2 6
1 2 3
5 1 9
2 4
Sample Output 1 :
5 0
3 2
1 5
5 3
3 2 6
1 2 3
5 1 9
Explanation for Sample Input 1 :
For test case 1:
Given matrix 'B' is [ [5, 0, 3, 2], [1, 5, 5, 3] ]. Converting it to a matrix of size 4*2 will look as 'S' = [ [5, 0], [3, 2], [1, 5], [5, 3] ].

For test case 2:
After arrangement on the shelf, one toy (with toy ID = 9) remains in the box. Shown in the below image.

Hence return the given matrix 'B' as the final answer.
Sample Input 2 :
2
3 3
4 -1 3
2 2 -2
1 3 1
3 5
3 4
5 2 4 7
6 3 4 2
3 7 5 6
2 6
Sample Output 2 :
4 -1 3
2 2 -2
1 3 1
5 2 4 7 6 3
4 2 3 7 5 6
Hint

Do you know how to map 1-d indices into 2-d and vice versa?

Approaches (1)
Implementation

 

Approach:
 

Assume 0-based indexing to understand the following:
 

  • If we need 'K'-th element (in row order traversal) from a matrix 'B' of size 'N*M', the element will be present at 'B[K / M][K % M]'. Look at the following example:
    • Let we have a matrix MAT = [ [5, 1, 3], [2, 0, 9], [7, 6, 8] ] of size 3*3.
    • Position of the second element (start counting from zero) in the matrix will be: MAT[K / M][K % M] = MAT[2/3][2%3] = MAT[0][2].
    • Position of the sixth element in the matrix will be: MAT[K / M][K % M] = MAT[6/3][6%3] = MAT[2][0].
    • Position of the eighth element in the matrix will be: MAT[K / M][K % M] = MAT[8/3][8%3] = MAT[2][2].
  • If 'N*M' is not equal to 'R*C', either a toy remains in the box, or a cell remains empty on the shelf. Hence return the original matrix 'B' as our answer.
  • Create an empty matrix 'ans' of size 'R*C'.
  • For each 'K'-th element (from 0 to 'N*M-1' each), use the above formula to find the corresponding positions in both the matrices and fill value in the 'ans'.

 

Algorithm:

 

Assume 0-based indexing to understand the following:

  • If 'N*M' is not equal to 'R*C', return the original matrix 'B'.
  • Create an empty matrix 'ans' of size 'R*C'.
  • Run a loop for 'i' = 0 to 'N*M-1'.
    • Position of the i-th element in the original matrix will be 'B[i/M][i%M]'.
    • Position of the i-th element in the created matrix will be 'ans[i/C][i%C]'.
    • Assign 'ans[i/C][i%C]' = 'B[i/M][i%M]'.
  • Return 'ans' as our final answer.
Time Complexity

 

O(N * M), where 'N' and 'M' are the number of rows and columns in matrix 'B', respectively.

 

All elements in the given matrix 'B' are traversed exactly once. Hence overall time complexity becomes O(N * M).

Space Complexity

 

O(1)

 

We use only constant space to solve. Hence the overall space complexity becomes O(1).

(A matrix of size 'R*C' is created, but that's for returning the answer, not a part of the solution).

Code Solution
(100% EXP penalty)
Reshape the Matrix
Full screen
Console