Construct a Binary Matrix whose sum of each row and column is a Prime Number
Last Updated :
30 Apr, 2021
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++
#include <bits/stdc++.h>
using namespace std;
void constructBinaryMat( int N)
{
int mat[N][N];
memset (mat, 0, sizeof (mat));
for ( int i = 0; i < N; i++) {
mat[i][i] = 1;
}
for ( int i = 0; i < N; i++) {
mat[i][N - i - 1] = 1;
}
if (N % 2 != 0) {
mat[N / 2][0] = 1;
mat[0][N / 2] = 1;
}
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
cout << mat[i][j] << " " ;
}
cout << endl;
}
}
int main()
{
int N = 5;
constructBinaryMat(N);
return 0;
}
|
Java
class GFG{
static void constructBinaryMat( int N)
{
int mat[][] = new int [N][N];
for ( int i = 0 ; i < N; i++)
{
mat[i][i] = 1 ;
}
for ( int i = 0 ; i < N; i++)
{
mat[i][N - i - 1 ] = 1 ;
}
if (N % 2 != 0 )
{
mat[N / 2 ][ 0 ] = 1 ;
mat[ 0 ][N / 2 ] = 1 ;
}
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
{
System.out.print(mat[i][j] + " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int N = 5 ;
constructBinaryMat(N);
}
}
|
Python3
def constructBinaryMat(N):
mat = [[ 0 for i in range (N)]
for i in range (N)]
for i in range (N):
mat[i][i] = 1
for i in range (N):
mat[i][N - i - 1 ] = 1
if (N % 2 ! = 0 ):
mat[N / / 2 ][ 0 ] = 1
mat[ 0 ][N / / 2 ] = 1
for i in range (N):
for j in range (N):
print (mat[i][j], end = " " )
print ()
if __name__ = = '__main__' :
N = 5
constructBinaryMat(N)
|
C#
using System;
class GFG{
static void constructBinaryMat( int N)
{
int [,]mat = new int [N, N];
for ( int i = 0; i < N; i++)
{
mat[i, i] = 1;
}
for ( int i = 0; i < N; i++)
{
mat[i, N - i - 1] = 1;
}
if (N % 2 != 0)
{
mat[N / 2, 0] = 1;
mat[0, N / 2] = 1;
}
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
Console.Write(mat[i, j] + " " );
}
Console.WriteLine();
}
}
public static void Main(String[] args)
{
int N = 5;
constructBinaryMat(N);
}
}
|
Javascript
<script>
function constructBinaryMat(N)
{
var mat = Array.from(Array(N), () =>
Array(N).fill(0));
for ( var i = 0; i < N; i++)
{
mat[i][i] = 1;
}
for ( var i = 0; i < N; i++)
{
mat[i][N - i - 1] = 1;
}
if (N % 2 != 0)
{
mat[parseInt(N / 2)][0] = 1;
mat[0][parseInt(N / 2)] = 1;
}
for ( var i = 0; i < N; i++)
{
for ( var j = 0; j < N; j++)
{
document.write( mat[i][j] + " " );
}
document.write( "<br>" );
}
}
var N = 5;
constructBinaryMat(N);
</script>
|
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)
Share your thoughts in the comments
Please Login to comment...