Techno Blender
Digitally Yours.

Construct a Matrix whose each row and column contains N and M 1s respectively

0 49


Improve Article

Save Article

Like Article

Improve Article

Save Article

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;

}

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:


Improve Article

Save Article

Like Article

Improve Article

Save Article

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;

}

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:

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment