BudiBadu Logo

Coral Pulse Count

Array Easy 1 views
Like9

Every full tide on Azure Reef, divers gather around a ring of luminous coral. The celebration opens with one radiant pulse at the center of the reef. Before the tide shifts, a herald declares how many ripple layers will follow. Each new layer introduces a fresh pulse to mark the boundary, and the coral from the previous layer echoes three times through the reefs lattice, sending light outward like a heartbeat felt in every direction.

The community treats this rhythm as a pact with the sea. Once the boundary pulse flashes, the reef repeats every earlier shimmer in triplicate, creating overlapping waves that help fishers navigate and keep nearby lagoon traffic aware of currents. The pattern is slow, deliberate, and always identical: one guiding pulse, three echoes of the past.

Your task is to compute the total number of pulses that glow by the time the final layer finishes. The input, layers, is a non-negative integer. If layers equals zero, only the central pulse shines. For each additional layer, include one new boundary pulse and triple the entire glow from the previous layer. Return the resulting number of pulses as an integer.

Example 1:

Input: layers = 0
Output: 1
Explanation: Only the initial coral emits light.

Example 2:

Input: layers = 1
Output: 4
Explanation: The first ripple adds one pulse and repeats the earlier glow three times.

Example 3:

Input: layers = 3
Output: 40
Explanation: Three layers maintain the rule, producing forty pulses when the celebration ends.

Algorithm Flow

Recommendation Algorithm Flow for Coral Pulse Count - Budibadu
Recommendation Algorithm Flow for Coral Pulse Count - Budibadu

Best Answers

java
class Solution {
    public int coral_pulse_count(Object layers) {
        int n = layers instanceof Integer ? (int) layers : 0;
        if (n == 0) return 1;
        int pulses = 1;
        for (int i = 0; i < n; i++) {
            pulses = 1 + 3 * pulses;
        }
        return pulses;
    }
}