Cascade Step Chimes
This one is about reading carefully and then following a clear rule. In Cascade Step Chimes, you are trying to work toward the right number by following one clear idea.
Calculate cascade growth pattern for chimes A good way to think about it is to first understand what goes in, then what rule you must follow, and finally what shape the answer should have.
For example, if the input is steps = 4, the answer is 161. Four additional steps mean another guiding chime plus a tripled echo of everything below, totaling 161 chimes. Another example is steps = 0, which gives 1. Only the base chime plays, so the crowd hears a single note.
This is a friendly practice problem, but it still rewards careful reading. The key is understanding the rule clearly and then applying it carefully.
One helpful habit is to say the rule out loud in your own words before you start solving. If you can explain what counts, what changes, and what the final answer should look like, you are already much closer to the right solution.
Example Input & Output
Four additional steps mean another guiding chime plus a tripled echo of everything below, totaling 161 chimes.
Only the base chime plays, so the crowd hears a single note.
The second step adds one guiding chime and repeats the prior soundscape three times, ending with seventeen chimes.
Algorithm Flow
Solution Approach
This problem asks us to count how many steps are active in the input array. An active step is any value that is not 0, so the answer is simply the count of non-zero elements.
The approach is straightforward: loop through the array and increment a counter whenever an element is not equal to 0. We do not change the values or their order, we only count them.
Here is the implementation:
steps.filter(step => step !== 0) keeps only the non-zero elements, and .length gives their count. Every zero is filtered out, and every non-zero value contributes one to the result.
For example, steps = [0, 3, 0, 7, 0] has two non-zero values, so the answer is 2. If the array is empty, the result is 0.
The time complexity is O(n) because we visit each element once, and the space complexity is O(n) because filter builds a new array (we could also count with a loop in O(1) space).
Best Answers
class Solution {
public int cascade_step_chimes(int[] steps) {
int count = 0;
for (int step : steps) {
if (step != 0) {
count++;
}
}
return count;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
