Dado un entero positivo N , la tarea es imprimir una array en zig-zag N × N que consta de números del 1 al N 2 , de modo que el recorrido en ZigZag de la array produzca el número en orden ascendente. Ejemplos:
Entrada: N = 3
Salida:
1 2 4
3 5 7
6 8 9
Explicación:
Entrada: N = 4
Salida:
1 2 4 7
3 5 8 11
6 9 12 14
10 13 15 16
Enfoque:
La array requerida se puede dividir en dos triángulos rectángulos.
- Un triángulo rectángulo invertido (considerado como un triángulo superior).
- Un triángulo rectángulo normal (considerado como un triángulo inferior).
La idea es iterar dos bucles anidados para llenar el triángulo superior con los valores respectivos. Luego itere dos bucles anidados nuevamente para llenar el triángulo inferior con los valores respectivos. Después de las dos operaciones anteriores, imprima la array deseada.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the pattern void printPattern(int n) { // N * N matrix to store the // values int arr[n][n]; arr[0][0] = 1; // Fill the values of // upper triangle for (int i = 0; i < n; i++) { if (i > 0) { arr[i][0] = arr[i - 1][0] + i + 1; } for (int j = 1; j < n - i; j++) { arr[i][j] = arr[i][j - 1] + i + j; } } // Fill the values of // lower triangle arr[1][n - 1] = arr[n - 1][0] + 1; int div = 0; for (int i = 2; i < n; i++) { div = n - 2; for (int j = n - i; j < n; j++) { if (j == n - i) { arr[i][j] = arr[i - 1][j + 1] + 1; } else { arr[i][j] = arr[i][j - 1] + div; div--; } } } // Print the array for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << arr[i][j] << " "; } cout << "\n"; } } // Driver Code int main() { // Given size of matrix int N = 4; // Function Call printPattern(N); return 0; }
Java
// Java program for // the above approach import java.util.*; class GFG{ // Function to print the pattern static void printPattern(int n) { // N * N matrix to store the // values int [][]arr = new int[n][n]; arr[0][0] = 1; // Fill the values of // upper triangle for (int i = 0; i < n; i++) { if (i > 0) { arr[i][0] = arr[i - 1][0] + i + 1; } for (int j = 1; j < n - i; j++) { arr[i][j] = arr[i][j - 1] + i + j; } } // Fill the values of // lower triangle arr[1][n - 1] = arr[n - 1][0] + 1; int div = 0; for (int i = 2; i < n; i++) { div = n - 2; for (int j = n - i; j < n; j++) { if (j == n - i) { arr[i][j] = arr[i - 1][j + 1] + 1; } else { arr[i][j] = arr[i][j - 1] + div; div--; } } } // Print the array for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(arr[i][j] + " "); } System.out.print("\n"); } } // Driver Code public static void main(String[] args) { // Given size of matrix int N = 4; // Function Call printPattern(N); } } // This code is contributed by Princi Singh
Python3
# Python3 program for the above approach # Function to print the pattern def printPattern(n): # N * N matrix to store the values arr = [[0 for i in range(n)] for j in range(n)] # Fill the values of upper triangle arr[0][0] = 1 for i in range(n): if i > 0: arr[i][0] = arr[i - 1][0] + i + 1 for j in range(1, n-i): arr[i][j] = arr[i][j - 1] + i + j # Fill the values of lower triangle if n > 1: arr[1][n - 1] = arr[n - 1][0] + 1 div = 0 for i in range(2, n): div = n-2 for j in range(n-i, n): if j == n-i: arr[i][j] = arr[i - 1][j + 1] + 1 else: arr[i][j] = arr[i][j - 1] + div div -= 1 # Print the array for i in range(n): for j in range(n): print(arr[i][j], end=' ') print("") # Driver code # Given size of matrix N = 4 # Function Call printPattern(N)
C#
// C# program for // the above approach using System; class GFG{ // Function to print the pattern static void printPattern(int n) { // N * N matrix to store the // values int [,]arr = new int[n, n]; arr[0,0] = 1; // Fill the values of // upper triangle for (int i = 0; i < n; i++) { if (i > 0) { arr[i, 0] = arr[i - 1, 0] + i + 1; } for (int j = 1; j < n - i; j++) { arr[i, j] = arr[i, j - 1] + i + j; } } // Fill the values of // lower triangle arr[1, n - 1] = arr[n - 1, 0] + 1; int div = 0; for (int i = 2; i < n; i++) { div = n - 2; for (int j = n - i; j < n; j++) { if (j == n - i) { arr[i, j] = arr[i - 1, j + 1] + 1; } else { arr[i, j] = arr[i, j - 1] + div; div--; } } } // Print the array for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(arr[i, j] + " "); } Console.Write("\n"); } } // Driver Code public static void Main(String[] args) { // Given size of matrix int N = 4; // Function Call printPattern(N); } } // This code is contributed by shikhasingrajput
Javascript
<script> // Javascript program for // the above approach // Function to print the pattern function printPattern(n) { // N * N matrix to store the // values let arr = new Array(n); // Loop to create 2D array using 1D array for (let i = 0; i < arr.length; i++) { arr[i] = new Array(2); } arr[0][0] = 1; // Fill the values of // upper triangle for (let i = 0; i < n; i++) { if (i > 0) { arr[i][0] = arr[i - 1][0] + i + 1; } for (let j = 1; j < n - i; j++) { arr[i][j] = arr[i][j - 1] + i + j; } } // Fill the values of // lower triangle arr[1][n - 1] = arr[n - 1][0] + 1; let div = 0; for (let i = 2; i < n; i++) { div = n - 2; for (let j = n - i; j < n; j++) { if (j == n - i) { arr[i][j] = arr[i - 1][j + 1] + 1; } else { arr[i][j] = arr[i][j - 1] + div; div--; } } } // Print the array for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { document.write(arr[i][j] + " "); } document.write("<br/>"); } } // Driver code // Given size of matrix let N = 4; // Function Call printPattern(N); // This code is contributed by susmitakundugoaldanga. </script>
1 2 4 7 3 5 8 11 6 9 12 14 10 13 15 16
Tiempo Complejidad: O(N 2 )
Espacio Auxiliar: O(N 2 )
Publicación traducida automáticamente
Artículo escrito por kundudinesh007 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA