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++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to construct grid with N and M void constructGrid( int N, int M) { if (N == 0 && M == 0) { cout << 0 << endl; } if (N == 0 || M == 0) { cout << "Grid not possible." << endl; } // Construct the grid vector<vector< int > > grid(M, vector< int >(N, 1)); // Print the grid for ( int i = 0; i < M; i++) { for ( int j = 0; j < N; j++) { cout << grid[i][j] << " " ; } cout << endl; } } // Driver Code int main() { // Testcase1 int N = 5, M = 4; // Function call cout << N << " " << M << endl; constructGrid(N, M); cout << endl; // Testcase2 N = 3, M = 7; cout << N << " " << M << endl; constructGrid(N, M); return 0; } |
Java
// Java code to implement the approach import java.io.*; class GFG { // Function to construct grid with N and M 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" ); } // Construct the grid int [][] grid = new int [M][N]; // Print the grid 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) { // Testcase1 int N = 5 , M = 4 ; // Function call System.out.println(N + " " + M); constructGrid(N, M); System.out.println(); // Testcase2 N = 3 ; M = 7 ; // Function call System.out.println(N + " " + M); constructGrid(N, M); } } // This code is contributed by lokesh |
Python3
# Python code to implement the approach # Function to construct grid with N and M def constructGrid(N, M): if (N = = 0 and M = = 0 ): print ( 0 ) if (N = = 0 or M = = 0 ): print ( "Grid not possible." ) # Construct the grid grid = [[ 1 for i in range (N)] for j in range (M)] # Print the grid for i in range (M): for j in range (N): print (grid[i][j], end = " " ) print () # Driver Code if __name__ = = '__main__' : # Testcase1 N = 5 M = 4 # Function call print (N, M) constructGrid(N, M) print () # Testcase2 N = 3 M = 7 print (N, M) constructGrid(N, M) # This code is contributed by Tapesh(tapeshdua420) |
C#
// C# code to implement the approach using System; public class GFG { // Function to construct grid with N and M 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" ); } // Construct the grid int [, ] grid = new int [M, N]; // Print the grid 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() { // Code // Testcase1 int N = 5, M = 4; // Function call Console.WriteLine(N + " " + M); constructGrid(N, M); Console.WriteLine(); // Testcase2 N = 3; M = 7; // Function call Console.WriteLine(N + " " + M); constructGrid(N, M); } } // This code is contributed by lokeshmvs21. |
Javascript
// JavaScript code to implement the approach // Function to construct grid with N and M function constructGrid(N, M) { if (N == 0 && M == 0) { console.log(0); } if (N == 0 || M == 0) { console.log( "Grid not possible." ); } // Construct the grid 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; } } // Print the grid for ( var i = 0; i < M; i++) { for ( var j = 0; j < N; j++) { process.stdout.write(grid[i][j]+ " " ); } console.log(); } } // Driver Code // Testcase1 var N = 5; var M = 4; // Function call console.log(N, M); constructGrid(N, M); console.log(); // Testcase2 N = 3; M = 7; console.log(N, M); constructGrid(N, M); // This code is contributed by Tapesh(tapeshdua420) |
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: