medium
0 views

Increment & Decrement Triangle Pattern

Print a triangle pattern where each line contains hyphens and integers in an incrementing and decrementing sequence, with 0 at the center.

Understand the Problem

Problem Statement

Increment & Decrement Triangle Pattern: The program must accept an integer N as the input. The program must print hyphens and integers in N+1 lines based on the following conditions:

  • In the 1st line, print N hyphens and an integer (0).
  • In the 2nd line, print N-1 hyphens and three integers (N, 0, N).
  • In the 3rd line, print N-2 hyphens and five integers (N-1, N, 0, N, N-1).
  • In the 4th line, print N-3 hyphens and seven integers (N-2, N-1, N, 0, N, N-1, N-2).
  • Continue this pattern until all N+1 lines are printed.

Constraints

  • 1 <= N <= 1000
  • All integers printed will be in the range [0, N]
  • Each line must have exactly one 0 at the center
  • The number of integers in line i is (2*i - 1)
  • The number of hyphens in line i is (N - i + 1)

Examples

Example 1
Input
5
Output
-----0
----505
---45054
--3450543
-234505432
12345054321
Explanation

For N=5:

  • Line 1: 5 hyphens + 0
  • Line 2: 4 hyphens + 5,0,5
  • Line 3: 3 hyphens + 4,5,0,5,4
  • Line 4: 2 hyphens + 3,4,5,0,5,4,3
  • Line 5: 1 hyphen + 2,3,4,5,0,5,4,3,2
  • Line 6: 1,2,3,4,5,0,5,4,3,2,1
Example 2
Input
9
Output
---------0
--------909
-------89098
------7890987
-----678909876
----56789098765
---4567890987654
--345678909876543
-23456789098765432
1234567890987654321
Explanation

For N=9:

  • Line 1: 9 hyphens + 0
  • Line 2: 8 hyphens + 9,0,9
  • Line 3: 7 hyphens + 8,9,0,9,8
  • ...continuing the symmetric pattern...
  • Line 10: 1,2,3,4,5,6,7,8,9,0,9,8,7,6,5,4,3,2,1
Example 3
Input
3
Output
---0
--303
-23032
1230321
Explanation

For N=3:

  • Line 1: 3 hyphens + 0
  • Line 2: 2 hyphens + 3,0,3
  • Line 3: 1 hyphen + 2,3,0,3,2
  • Line 4: 1,2,3,0,3,2,1

Solution

#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    
    // First line: only hyphens and 0
    for (int i = 0; i < n; i++) {
        printf("-");
    }
    printf("0\n");
    
    // Remaining lines
    for (int i = n; i >= 1; i--) {
        // Print hyphens
        for (int j = 0; j < i - 1; j++) {
            printf("-");
        }
        
        // Print left side numbers
        for (int j = i; j <= n; j++) {
            printf("%d", j);
        }
        
        // Print center 0
        printf("0");
        
        // Print right side numbers
        for (int j = n; j >= i; j--) {
            printf("%d", j);
        }
        
        printf("\n");
    }
    
    return 0;
}
Time:O(N²) - We have nested loops where the outer loop runs N times and inner loops run proportionally to N.
Space:O(1) - Only using a constant amount of extra space for variables.
Approach:

C Solution Explanation:

  1. First line: Print N hyphens followed by 0.
  2. Main loop: Iterate from N down to 1.
  3. Hyphens: Print (i-1) hyphens for each line.
  4. Left side: Print numbers from i to N in ascending order.
  5. Center: Print 0.
  6. Right side: Print numbers from N down to i in descending order.
  7. New line: Move to next line after each iteration.

Visual Explanation

Loading diagram...