Construct a Matrix whose each row and column contains N and M 1s respectively
Construct a matrix whose each row contains N 1’s and each column contains M 1’s.
Examples:
Input: N = 3, M = 4
Output:
1 1 1
1 1 1
1 1 1
1 1 1
Explanation: Each row contains N’s 1 and each column contains M’s 1.Input: N = 0, M = 0
Output: 0
Approach:
Follow the below steps to solve the problem:
- If N and M are 0 then simply print 0.
- If exactly one of N and M is 0, then it is impossible to make the grid.
- Else make the 2D vector or array of dimension MXN.
- Print the vector/array using nested loops.
Below is the implementation of the above approach.
C++
|
5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Related Articles:
Construct a matrix whose each row contains N 1’s and each column contains M 1’s.
Examples:
Input: N = 3, M = 4
Output:
1 1 1
1 1 1
1 1 1
1 1 1
Explanation: Each row contains N’s 1 and each column contains M’s 1.Input: N = 0, M = 0
Output: 0
Approach:
Follow the below steps to solve the problem:
- If N and M are 0 then simply print 0.
- If exactly one of N and M is 0, then it is impossible to make the grid.
- Else make the 2D vector or array of dimension MXN.
- Print the vector/array using nested loops.
Below is the implementation of the above approach.
C++
|
5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Related Articles: