medium
0 views

Height Pattern Printing

Generate a graphical column chart using '#' and '-' characters based on a given list of integer heights.

Understand the Problem

Problem Statement

You are given the heights of N cats. Your task is to print a graphical representation of these heights as a vertical column chart.

The chart should be constructed using the # character to represent the bars and the - character for empty space, as shown in the examples.

Constraints

  • 2 <= N <= 100 (Number of cats)
  • 1 <= Height of each cat <= 100

Examples

Example 1
Input
6
10 4 2 6 7 2
Output
# - - - - - 
# - - - - - 
# - - - - - 
# - - - # - 
# - - # # - 
# - - # # - 
# # - # # - 
# # - # # - 
# # # # # # 
# # # # # # 
Explanation

The maximum height is 10, so 10 rows are printed. The first cat has height 10, so it gets a '#' in all 10 rows. The second cat has height 4, so it gets a '#' only in the bottom 4 rows (when the height level is 1, 2, 3, or 4), and so on.

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

The maximum height is 10. The heights increase from 1 to 10. This creates a diagonal pattern. The last cat (height 10) has a '#' in every row, while the first cat (height 1) only has a '#' in the very last row (when the height level is 1).

Solution

#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    int heights[n];
    int max_height = 0;

    for (int i = 0; i < n; i++) {
        scanf("%d", &heights[i]);
        if (heights[i] > max_height) {
            max_height = heights[i];
        }
    }

    for (int level = max_height; level >= 1; level--) {
        for (int i = 0; i < n; i++) {
            if (heights[i] >= level) {
                printf("# ");
            } else {
                printf("- ");
            }
        }
        printf("\n");
    }

    return 0;
}
Time:O(N * H)
Space:O(N)
Approach:
  1. Include Header: We include <stdio.h> for input/output functions.
  2. Read N: We read the number of cats, n, from the first line of input.
  3. Declare Array and Find Max: An integer array heights of size n is declared (using a Variable-Length Array). We also initialize max_height to 0. We then loop n times to read each cat's height. Inside this loop, we update max_height if the current height is greater than the current max.
  4. Outer Loop (Rows): The first for loop iterates from level = max_height down to 1. This loop controls which row of the chart is currently being printed.
  5. Inner Loop (Columns): The second, nested for loop iterates from i = 0 to n-1. This loop goes through each cat for the current row (level).
  6. Print Character: Inside the inner loop, an if condition checks if the height of the current cat (heights[i]) is greater than or equal to the current level. If it is, # is printed; otherwise, - is printed.
  7. Newline: After the inner loop completes, printf("\n"); is called to move the cursor to the next line for the next row of the chart.

Visual Explanation

Loading diagram...