Terraced Shell Count
Picture a shell display that keeps growing in little terraces. At the very beginning there is only one shell. Then each new terrace makes the pattern larger by building on the whole shape that came before it.
Your job is to find out how many shells appear after a certain number of tiers. This is a recursive growth puzzle, which means each new answer depends on the earlier answer. The pattern does not restart from zero every time. It grows from the last stage and adds more structure around it.
For example, when tiers = 0, the answer is 1. When tiers = 1, the display grows to 6. By the time tiers = 3, it reaches 36. So the numbers grow quickly, and the important part is seeing how one stage leads to the next.
The base case matters here because it tells you where the whole pattern begins. After that, each new tier uses the earlier total to make a larger one. So the real challenge is finding the rule that connects one tier count to the next.
Example Input & Output
Three additional terraces mean another guiding shell plus twin reflections of everything from tier two.
Only the original shell is shown.
The new terrace adds one guiding shell and mirrors the earlier display twice.
Algorithm Flow
Solution Approach
This problem is a recursive growth pattern where the shell display grows by building on its previous shape. The totals grow quickly, which tells us the pattern involves repeated multiplication rather than a steady increase.
Looking at the examples, the values are 1, 6, 16, 36, 76 for tiers 0 through 4. The gaps between consecutive values are +5, +10, +20, +40, and each gap is double the previous one. This doubling suggests a connection to powers of two.
With a base of 1 and gaps of 5, 10, 20, ..., the total after tiers can be written directly as 5 * 2^tiers - 4. We can verify this against every example before implementing.
Let us check the formula. When tiers = 0, we get 5 * 1 - 4 = 1. When tiers = 1, we get 5 * 2 - 4 = 6. When tiers = 2, we get 5 * 4 - 4 = 16. When tiers = 3, we get 5 * 8 - 4 = 36. And when tiers = 4, we get 5 * 16 - 4 = 76. Every value matches the expected output.
The insight behind the formula is that each new tier doubles the previous structure and adds a fixed amount, so the total is essentially an exponential function of the tier count with a constant offset. This is why the numbers grow so quickly.
Because we compute the answer with a single expression, the time complexity is O(1) and the space complexity is O(1) — far better than simulating every tier.
Best Answers
class Solution {
public int calculate_shell_count(int n) {
return 5 * (1 << n) - 4;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
