Code Logo

Harbor Tide Chants

Published atDate not available
Easy 0 views
Like0

Think of a small harbor challenge where order and timing really matter. In Harbor Tide Chants, you are trying to work toward the right number by following one clear idea.

Generate harbor tide chant recursively 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 rolls = 2, the answer is 13. The second roll adds a verse and repeats the earlier chorus three times. Another example is rolls = 0, which gives 1. Only the caller sings, so one verse is heard.

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

Example 1
Input
rolls = 2
Output
13
Explanation

The second roll adds a verse and repeats the earlier chorus three times.

Example 2
Input
rolls = 0
Output
1
Explanation

Only the caller sings, so one verse is heard.

Example 3
Input
rolls = 4
Output
121
Explanation

Four rolls follow the rule, ending with one guiding verse plus triple the sound of roll three.

Algorithm Flow

Recommendation Algorithm Flow for Harbor Tide Chants
Recommendation Algorithm Flow for Harbor Tide Chants

Best Answers

java
class Solution {
    public int harbor_tide_chants(int rolls) {
        int result = 1;
        for (int i = 0; i < rolls; i++) {
            result = 1 + 3 * result;
        }
        return result;
    }
}