medium
0 views

Vertical ZigZag Pattern

Print a vertical zigzag pattern of numbers in a matrix format

Understand the Problem

Problem Statement

Fill in the missing lines of code to print the pattern as defined in the Example Input/Output section.

Input Format:
The first line contains N.

Output Format:
N lines contain the number pattern.

Example Input/Output 1:
Input:
5

Output:
1
2 9  
3 8 10
4 7 11 14
5 6 12 13 15

Example Input/Output 2:
Input:
4

Output:
1
2 7
3 6 8
4 5 9 10

Constraints

  • 1 ≤ N ≤ 50
  • Output should be a vertical zigzag pattern
  • Each row should contain only the numbers that belong to that diagonal
  • No trailing spaces at the end of lines

Examples

Example 1
Input
5
Output
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
Explanation

For N=5, the pattern creates a 5×5 matrix filled with numbers 1-15. The even columns (0, 2, 4) are filled top-to-bottom, while odd columns (1, 3) are filled bottom-to-top. When printed diagonally, this creates the vertical zigzag pattern shown.

Example 2
Input
4
Output
1
2 7
3 6 8
4 5 9 10
Explanation

For N=4, the pattern creates a 4×4 matrix filled with numbers 1-10. The zigzag effect is created by alternating the fill direction for each column, then printing the matrix diagonally.

Solution

#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    
    // Calculate total numbers needed
    int total = n * (n + 1) / 2;
    
    // Create N×N matrix initialized with zeros
    int matrix[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            matrix[i][j] = 0;
        }
    }
    
    int counter = 1;
    int row = 0;
    int col = 0;
    
    // Fill the matrix column by column
    for (col = 0; col < n; col++) {
        if (col % 2 == 0) {
            // Even columns: fill from top to bottom
            for (row = col; row < n; row++) {
                matrix[row][col] = counter++;
            }
        } else {
            // Odd columns: fill from bottom to top
            for (row = n - 1; row >= col; row--) {
                matrix[row][col] = counter++;
            }
        }
    }
    
    // Print the matrix diagonally
    for (row = 0; row < n; row++) {
        for (col = 0; col <= row; col++) {
            if (matrix[col][row] != 0) {
                printf("%d ", matrix[col][row]);
            }
        }
        printf("\n");
    }
    
    return 0;
}
Time:O(N²) - We fill an N×N matrix and then print it diagonally
Space:O(N²) - We use an N×N matrix to store the pattern
Approach:

The C solution follows the same approach as described in the general approach section:

  1. Input Reading: Read integer N using scanf
  2. Matrix Creation: Create a 2D array of size N×N and initialize all elements to 0
  3. Matrix Filling: Use a counter starting from 1 to fill the matrix column by column:
    • For even columns (0, 2, 4...): iterate from top to bottom
    • For odd columns (1, 3, 5...): iterate from bottom to top
  4. Output: Print the matrix by reading it diagonally from top-left to bottom-right

The key insight is that by alternating the fill direction for each column, we create the zigzag pattern when the matrix is printed diagonally.

Visual Explanation

Loading diagram...