Construct a Binary Matrix whose sum of each row and column is a Prime Number

Given an integer N, the task is to construct a binary matrix of size N * N such that the sum of each row and each column of the matrix is a prime number.

Examples:

Input: N = 2 
Output: 

1 1
1 1

Explanation: 
Sum of 0th row = 1 + 1 = 2 (Prime number) 
Sum of 1st row = 1 + 1 = 2 (Prime number) 
Sum of 0th column = 1 + 1 = 2 (Prime number) 
Sum of 1st column = 1 + 1 = 2 (Prime number)

Input: N = 4 
Output: 



1 0 0 1 
0 1 1 0 
0 1 1 0 
1 0 0 1 

Approach: Follow the steps below to solve the problem:

  • Initialize a Binary matrix, say mat[][] of size N * N.
  • Update all possible values of mat[i][i] to 1.
  • Update all possible values of mat[i][N – i -1] to 1.
  • If N is an odd number then update the value mat[N / 2][0] and mat[0][N / 2] to 1.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to construct
// the required binary matrix
void constructBinaryMat(int N)
{
    // Stores binary value with row
    // and column sum as prime number
    int mat[N][N];
 
    // initialize the binary matrix mat[][]
    memset(mat, 0, sizeof(mat));
 
    // Update all possible values of
    // mat[i][i] to 1
    for (int i = 0; i < N; i++) {
        mat[i][i] = 1;
    }
 
    // Update all possible values of
    // mat[i][N - i -1]
    for (int i = 0; i < N; i++) {
        mat[i][N - i - 1] = 1;
    }
 
    // Check if N is an odd number
    if (N % 2 != 0) {
 
        // Update mat[N / 2][0] to 1
        mat[N / 2][0] = 1;
 
        // Update mat[0][N / 2] to 1
        mat[0][N / 2] = 1;
    }
 
    // Print required binary matrix
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    int N = 5;
 
    constructBinaryMat(N);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
class GFG{
 
// Function to construct
// the required binary matrix
static void constructBinaryMat(int N)
{
  // Stores binary value with row
  // and column sum as prime number
  int mat[][] = new int[N][N];
 
  // Update all possible values
  // of mat[i][i] to 1
  for (int i = 0; i < N; i++)
  {
    mat[i][i] = 1;
  }
 
  // Update all possible values
  // of mat[i][N - i -1]
  for (int i = 0; i < N; i++)
  {
    mat[i][N - i - 1] = 1;
  }
 
  // Check if N is an odd
  // number
  if (N % 2 != 0)
  {
    // Update mat[N / 2][0]
    // to 1
    mat[N / 2][0] = 1;
 
    // Update mat[0][N / 2]
    // to 1
    mat[0][N / 2] = 1;
  }
 
  // Print required binary matrix
  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < N; j++)
    {
      System.out.print(mat[i][j] + " ");
    }
    System.out.println();
  }
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 5;
  constructBinaryMat(N);
}
}
 
// This code is contributed by Chitranayal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to construct
# the required binary matrix
def constructBinaryMat(N):
     
    # Stores binary value with row
    # and column sum as prime number
    mat = [[0 for i in range(N)]
              for i in range(N)]
 
    # Initialize the binary matrix mat[][]
    # memset(mat, 0, sizeof(mat));
 
    # Update all possible values of
    # mat[i][i] to 1
    for i in range(N):
        mat[i][i] = 1
 
    # Update all possible values of
    # mat[i][N - i -1]
    for i in range(N):
        mat[i][N - i - 1] = 1
 
    # Check if N is an odd number
    if (N % 2 != 0):
         
        # Update mat[N / 2][0] to 1
        mat[N // 2][0] = 1
 
        # Update mat[0][N / 2] to 1
        mat[0][N // 2] = 1
 
    # Print required binary matrix
    for i in range(N):
        for j in range(N):
            print(mat[i][j], end = " ")
             
        print()
 
# Driver Code
if __name__ == '__main__':
     
    N = 5
 
    constructBinaryMat(N)
     
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to construct
// the required binary matrix
static void constructBinaryMat(int N)
{
  // Stores binary value with row
  // and column sum as prime number
  int [,]mat = new int[N, N];
 
  // Update all possible values
  // of mat[i,i] to 1
  for (int i = 0; i < N; i++)
  {
    mat[i, i] = 1;
  }
 
  // Update all possible values
  // of mat[i,N - i -1]
  for (int i = 0; i < N; i++)
  {
    mat[i, N - i - 1] = 1;
  }
 
  // Check if N is an odd
  // number
  if (N % 2 != 0)
  {
    // Update mat[N / 2,0]
    // to 1
    mat[N / 2, 0] = 1;
 
    // Update mat[0,N / 2]
    // to 1
    mat[0, N / 2] = 1;
  }
 
  // Print required binary matrix
  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < N; j++)
    {
      Console.Write(mat[i, j] + " ");
    }
    Console.WriteLine();
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 5;
  constructBinaryMat(N);
}
}
 
// This code is contributed by gauravrajput1

chevron_right


Output: 

1 0 1 0 1 
0 1 0 1 0 
1 0 1 0 0 
0 1 0 1 0 
1 0 0 0 1







 

Time Complexity: O(N2)
Auxiliary Space: O(N2)

competitive-programming-img




My Personal Notes arrow_drop_up

Recommended Posts:



If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.