Sandglass Pattern Generation Algorithm

This problem requires implementing a program that prints a sandglass pattern using a specified character. For instance, given 17 asterisks, the output should follow this format:

*****
 ***
  *
 ***
*****

A sandglass shape has these characteristics: each row contains an odd number of characters, all rows are center-aligned, adjacent rows differ by exactly 2 characters, the count decreases from maximum to 1 and then increases back, and the first and last rows have equal numbers of characters.

Given N characters, it may not always be possible to form a perfect sandglass. The goal is to create the largest possible sandglass using as many characters as feasible.

Input Format

A single line containing a positive integer N (≤1000) followed by a character, separated by a space.

Output Format

First, print the largest sandglass pattern possible with the given character. Then, output the remaining unused character count on a separate line.

Sample Input

19 *

Sample Output

*****
 ***
  *
 ***
*****
2

Solution Approach:

Let h represent the number of layers in the sandglass (always an odd number). The total character count follows the formula: (1+3+5+...+h)×2−1, which represents two triangular patterns minus one overlapping center character.

To determine the maximum usable layers, we solve for h where the total character requirement is less than or equal to N. When N exceeds this value, h represents the maximum constructible layers.

After determining h, the pattern construction involves two phases: printing from top to center (decreasing), then from center to bottom (increasing).

#include <iostream>
#include <cmath>
using namespace std;

int totalCount, layers;
char patternChar;

int main() {
    cin >> totalCount >> patternChar;
    
    // Calculate maximum layers possible
    layers = static_cast<int>(sqrt((totalCount + 1) / 2));
    
    // Print upper half including center row
    for (int currentRow = layers; currentRow >= 1; currentRow--) {
        // Print leading spaces
        for (int space = 0; space < layers - currentRow; space++) {
            cout << " ";
        }
        // Print pattern characters
        for (int star = 0; star < currentRow * 2 - 1; star++) {
            cout << patternChar;
        }
        cout << endl;
    }
    
    // Print lower half excluding center row
    for (int currentRow = 2; currentRow <= layers; currentRow++) {
        // Print leading spaces
        for (int space = 0; space < layers - currentRow; space++) {
            cout << " ";
        }
        // Print pattern characters
        for (int star = 0; star < currentRow * 2 - 1; star++) {
            cout << patternChar;
        }
        cout << endl;
    }
    
    // Calculate and output remaining characters
    int usedCharacters = 2 * layers * layers - 1;
    cout << totalCount - usedCharacters << endl;
    
    return 0;
}

Tags: algorithm pattern-generation mathematical-computing C++

Posted on Sat, 09 May 2026 07:15:06 +0000 by Jalz