We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
It's simple. When n=3, the patten will be printed as below.
----c----
--c-b-c--
c-b-a-b-c
--c-b-c--
----c----
If you observe, there are 9 characters on each line. When you consider c-b-a-b-c (the line in the center), the number of characters is 1{for a}+2*(n-1){for b,c}+(2n-2){add the previous 2 and subtract 1 from it, because the number of '-' is one less than the number of characters in the line}. That's how we arrived at 4n-3.
Similarly when n=4, we'll have 13 characters and when n=5, we'll have 17. Hope this helped!
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Alphabet Rangoli
You are viewing a single comment's thread. Return to all comments →
It's simple. When n=3, the patten will be printed as below.
----c----
--c-b-c--
c-b-a-b-c
--c-b-c--
----c----
If you observe, there are 9 characters on each line. When you consider c-b-a-b-c (the line in the center), the number of characters is 1{for a}+2*(n-1){for b,c}+(2n-2){add the previous 2 and subtract 1 from it, because the number of '-' is one less than the number of characters in the line}. That's how we arrived at 4n-3. Similarly when n=4, we'll have 13 characters and when n=5, we'll have 17. Hope this helped!