Dado un número entero N , la tarea es construir una array binaria de tamaño N*N tal que la suma de cada fila y cada columna de la array sea un número primo .
Ejemplos:
Entrada: N = 2
Salida:1 1 1 1Explicación:
Suma de la fila 0 = 1 + 1 = 2 (Número primo)
Suma de la fila 1 = 1 + 1 = 2 (Número primo)
Suma de la columna 0 = 1 + 1 = 2 (Número primo)
Suma de 1 ª columna = 1 + 1 = 2 (Número primo)Entrada: N = 4
Salida:1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice una array binaria , digamos mat[][] de tamaño N * N.
- Actualice todos los valores posibles de mat[i][i] a 1 .
- Actualice todos los valores posibles de mat[i][N – i -1] a 1 .
- Si N es un número impar , actualice el valor mat[N/2][0] y mat[0][N/2] a 1 .
A continuación se muestra la implementación del enfoque anterior.
C++
// 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; }
Java
// 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
Python3
# 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
C#
// 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
Javascript
<script> // Javascript program to implement // the above approach // Function to construct // the required binary matrix function constructBinaryMat(N) { // Stores binary value with row // and column sum as prime number var mat = Array.from(Array(N), () => Array(N).fill(0)); // Update all possible values of // mat[i][i] to 1 for(var i = 0; i < N; i++) { mat[i][i] = 1; } // Update all possible values of // mat[i][N - i -1] for(var 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[parseInt(N / 2)][0] = 1; // Update mat[0][N / 2] to 1 mat[0][parseInt(N / 2)] = 1; } // Print required binary matrix for(var i = 0; i < N; i++) { for(var j = 0; j < N; j++) { document.write( mat[i][j] + " "); } document.write("<br>"); } } // Driver Code var N = 5; constructBinaryMat(N); // This code is contributed by rutvik_56 </script>
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
Tiempo Complejidad: O(N 2 )
Espacio Auxiliar: O(N 2 )
Publicación traducida automáticamente
Artículo escrito por mishrapriyanshu557 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA