New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Alphabet Rangoli Pattern

The alphabet rangoli is a well-known HackerRank problem: draw a rangoli, the decorative floor design from India, out of letters and dashes. For size 3 the letters run c, b, a towards the centre and back out again.

Quick answer: Loop over rows r from βˆ’(n βˆ’ 1) to n βˆ’ 1. Row r has d = n βˆ’ 1 βˆ’ |r| letters on each side of its middle letter. Build the letters from the n-th letter down towards the middle and back, join them with dashes, and centre the row in a width of 4n βˆ’ 3 with dashes.

This program is part of the String Patterns collection.

Table of contents8 sections
  1. Output
  2. How the alphabet rangoli pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Alphabet Rangoli Pattern output

Output (size 3):

----c----
--c-b-c--
c-b-a-b-c
--c-b-c--
----c----
Alphabet Rangoli Pattern layout diagram, row by row (size 3)
How each row of the alphabet rangoli pattern is built (size 3). Blue cells are printed characters; grey dots are spaces.

How the alphabet rangoli pattern works

The rangoli is a diamond in which each row is a palindrome of letters. Counting the rows from βˆ’(n βˆ’ 1) to n βˆ’ 1 makes the symmetry easy: the absolute value |r| is the distance from the middle row, so the top and bottom halves share one formula. Inside a row, the letter at offset t from the centre is 'a' + n βˆ’ 1 βˆ’ d + |t|: the centre letter is the smallest and the letters grow outwards. Every row is 4n βˆ’ 3 characters wide, so the dashes on each side make up the difference.

  1. Set width = 4n βˆ’ 3.
  2. For r from 1 βˆ’ n to n βˆ’ 1, set d = n βˆ’ 1 βˆ’ |r|.
  3. Build the letters for t from βˆ’d to d: 'a' + n βˆ’ 1 βˆ’ d + |t|, joined with dashes.
  4. Pad both sides with dashes to the full width and print the row.

Key takeaways

  • Counting rows from βˆ’(n βˆ’ 1) lets one formula draw both halves.
  • Each row is a palindrome with the smallest letter in the middle.
  • Every row is padded with dashes to 4n βˆ’ 3 characters.
  • Python’s str.center(width, "-") does the padding in one call.

Alphabet Rangoli Pattern in C

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int n = 3;
    int width = 4 * n - 3;

    for (int r = 1 - n; r <= n - 1; r++) {
        int d = n - 1 - abs(r);                 /* letters on each side of the middle one */
        int pad = (width - (4 * d + 1)) / 2;
        for (int k = 0; k < pad; k++)
            printf("-");
        for (int t = -d; t <= d; t++) {
            printf("%c", 'a' + n - 1 - d + abs(t));
            if (t < d)
                printf("-");
        }
        for (int k = 0; k < pad; k++)
            printf("-");
        printf("\n");
    }
    return 0;
}

Alphabet Rangoli Pattern in C++

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main() {
    int n = 3;
    int width = 4 * n - 3;

    for (int r = 1 - n; r <= n - 1; r++) {
        int d = n - 1 - abs(r);                 // letters on each side of the middle one
        int pad = (width - (4 * d + 1)) / 2;
        cout << string(pad, '-');
        for (int t = -d; t <= d; t++) {
            cout << char('a' + n - 1 - d + abs(t));
            if (t < d)
                cout << "-";
        }
        cout << string(pad, '-') << endl;
    }
    return 0;
}

Alphabet Rangoli Pattern in Java

public class AlphabetRangoli {
    public static void main(String[] args) {
        int n = 3;
        int width = 4 * n - 3;

        for (int r = 1 - n; r <= n - 1; r++) {
            int d = n - 1 - Math.abs(r);            // letters on each side of the middle one
            int pad = (width - (4 * d + 1)) / 2;
            StringBuilder row = new StringBuilder("-".repeat(pad));
            for (int t = -d; t <= d; t++) {
                row.append((char) ('a' + n - 1 - d + Math.abs(t)));
                if (t < d)
                    row.append("-");
            }
            row.append("-".repeat(pad));
            System.out.println(row);
        }
    }
}

Alphabet Rangoli Pattern in Python

n = 3
width = 4 * n - 3

for r in range(1 - n, n):
    d = n - 1 - abs(r)                      # letters on each side of the middle one
    letters = [chr(ord("a") + n - 1 - d + abs(t)) for t in range(-d, d + 1)]
    print("-".join(letters).center(width, "-"))

Time and space complexity

The pattern has 2n βˆ’ 1 rows of 4n βˆ’ 3 characters, so it runs in O(nΒ²) time. The Python version builds one row at a time, which needs O(n) memory.

Frequently asked questions

What does the size-5 rangoli look like?

It has 9 rows and is 17 characters wide. The middle row is e-d-c-b-a-b-c-d-e, and the first and last rows are ——–e——–.

Why is the width 4n βˆ’ 3?

The middle row has 2n βˆ’ 1 letters and 2n βˆ’ 2 dashes between them, which adds up to 4n βˆ’ 3. Every other row is padded to the same width.

Is this pattern asked outside HackerRank?

Yes, it appears in Python practice sets and placement tests, often with the same name. The idea of counting rows from a negative number is useful for any diamond, such as the diamond star pattern.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 18 of 22 in String Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.