Close
Topic list
Reshape the Matrix
EASY
10 mins
Matrices (2D Arrays)
Topics (Covered in this problem)
Problem solved
Skill meter
Matrices (2D Arrays)
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Reshape the Matrix

Contributed by
Himanshu Airan
Easy
0/40
Avg time to solve 10 mins
Success Rate 70 %
Share

## Problem Statement

#### 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, Constraints, Images )
##### 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
``````
Auto
Console