Construct a Matrix whose each row and column contains N and M 1s respectively
Last Updated :
14 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void constructGrid( int N, int M)
{
if (N == 0 && M == 0) {
cout << 0 << endl;
}
if (N == 0 || M == 0) {
cout << "Grid not possible." << endl;
}
vector<vector< int > > grid(M, vector< int >(N, 1));
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
cout << grid[i][j] << " " ;
}
cout << endl;
}
}
int main()
{
int N = 5, M = 4;
cout << N << " " << M << endl;
constructGrid(N, M);
cout << endl;
N = 3, M = 7;
cout << N << " " << M << endl;
constructGrid(N, M);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void constructGrid( int N, int M)
{
if (N == 0 && M == 0 ) {
System.out.println( 0 );
}
if (N == 0 || M == 0 ) {
System.out.println( "Grid not possible" );
}
int [][] grid = new int [M][N];
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++) {
grid[i][j] = 1 ;
System.out.print(grid[i][j] + " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int N = 5 , M = 4 ;
System.out.println(N + " " + M);
constructGrid(N, M);
System.out.println();
N = 3 ;
M = 7 ;
System.out.println(N + " " + M);
constructGrid(N, M);
}
}
|
Python3
def constructGrid(N, M):
if (N = = 0 and M = = 0 ):
print ( 0 )
if (N = = 0 or M = = 0 ):
print ( "Grid not possible." )
grid = [[ 1 for i in range (N)] for j in range (M)]
for i in range (M):
for j in range (N):
print (grid[i][j], end = " " )
print ()
if __name__ = = '__main__' :
N = 5
M = 4
print (N, M)
constructGrid(N, M)
print ()
N = 3
M = 7
print (N, M)
constructGrid(N, M)
|
C#
using System;
public class GFG {
static void constructGrid( int N, int M)
{
if (N == 0 && M == 0) {
Console.WriteLine(0);
}
if (N == 0 || M == 0) {
Console.WriteLine( "Grid not possible" );
}
int [, ] grid = new int [M, N];
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
grid[i, j] = 1;
Console.Write(grid[i, j] + " " );
}
Console.WriteLine();
}
}
static public void Main()
{
int N = 5, M = 4;
Console.WriteLine(N + " " + M);
constructGrid(N, M);
Console.WriteLine();
N = 3;
M = 7;
Console.WriteLine(N + " " + M);
constructGrid(N, M);
}
}
|
Javascript
function constructGrid(N, M) {
if (N == 0 && M == 0) {
console.log(0);
}
if (N == 0 || M == 0) {
console.log( "Grid not possible." );
}
var grid = new Array(M);
for ( var i = 0; i < M; i++) {
grid[i] = new Array(N);
}
for ( var i = 0; i < M; i++) {
for ( var j = 0; j < N; j++) {
grid[i][j] = 1;
}
}
for ( var i = 0; i < M; i++) {
for ( var j = 0; j < N; j++) {
process.stdout.write(grid[i][j]+ " " );
}
console.log();
}
}
var N = 5;
var M = 4;
console.log(N, M);
constructGrid(N, M);
console.log();
N = 3;
M = 7;
console.log(N, M);
constructGrid(N, M);
|
Output
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...