Code Logo

Canyon Echo Verse

Published at05 Jan 2026
Array Manipulation Easy 10 views
Like2

This challenge becomes much easier once you know exactly what to keep, change, or count. In Canyon Echo Verse, you are trying to work toward the right number by following one clear idea.

Generate recursive echo pattern for storytelling 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 layers = 0, the answer is 1. Only the narrator speaks, so one verse is heard. Another example is layers = 4, which gives 49. Four layers continue the same pattern, leading to forty-nine verses echoing through the canyon.

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
layers = 0
Output
1
Explanation

Only the narrator speaks, so one verse is heard.

Example 2
Input
layers = 2
Output
9
Explanation

The second layer adds a verse and repeats the earlier chorus twice.

Example 3
Input
layers = 4
Output
49
Explanation

Four layers continue the same pattern, leading to forty-nine verses echoing through the canyon.

Algorithm Flow

Recommendation Algorithm Flow for Canyon Echo Verse

Solution Approach

This problem asks us to repeat a verse a given number of times and join the copies with spaces. Each "echo" adds one more copy of the original verse, so the total number of copies is echoes + 1 (the original plus each echo).

The simplest approach is to create an array filled with the verse repeated echoes + 1 times, then join it with a single space between each copy.

Here is the implementation:

function canyon_echo_verse(verse, echoes) {
    return new Array(echoes + 1).fill(verse).join(' ');
}

new Array(echoes + 1) creates an array of the right length, .fill(verse) puts the verse in every slot, and .join(' ') connects the copies with spaces. The result is the verse repeated exactly echoes + 1 times.

For example, with verse = "Hello" and echoes = 2, we get "Hello Hello Hello" — the original plus two echoes. When echoes = 0, we get just "Hello".

The time and space complexity are both O(echoes), since we build and join an array of echoes + 1 elements.

Best Answers

java
import java.util.*;
class Solution {
    public String canyon_echo_verse(String verse, int echoes) {
        String[] parts = new String[echoes + 1];
        Arrays.fill(parts, verse);
        return String.join(" ", parts);
    }
}