Code Logo

Moonlit Orchid Bloom

Published at05 Jan 2026
Array Manipulation Easy 7 views
Like30

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

Calculate orchid bloom pattern 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 rings = 5, the answer is 63. Five rings follow the pattern, yielding sixty-three glowing orchids. Another example is rings = 0, which gives 1. Only the central orchid opens.

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
rings = 5
Output
63
Explanation

Five rings follow the pattern, yielding sixty-three glowing orchids.

Example 2
Input
rings = 0
Output
1
Explanation

Only the central orchid opens.

Example 3
Input
rings = 2
Output
7
Explanation

The second ring adds one blossom and reflects the earlier blooms twice.

Algorithm Flow

Recommendation Algorithm Flow for Moonlit Orchid Bloom

Solution Approach

This problem asks us to find the length of the longest contiguous increasing segment inside the array. A segment counts only while each element is strictly greater than the one before it; as soon as the trend breaks, the run restarts.

The key insight is that we do not need to compare every possible subarray. Instead, we scan the array once, extending the current run when the next value is larger, and resetting it otherwise. We track the best run we have seen.

Here is the implementation:

function calculate_orchid_height(arr) {
    if (arr.length === 0) return 0;
    let best = 1, current = 1;
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] > arr[i - 1]) {
            current++;
        } else {
            current = 1;
        }
        best = Math.max(best, current);
    }
    return best;
}

We start with both best and current set to 1, since a single element is always a valid run. As we walk the array, whenever the current value is greater than the previous one, we extend current; otherwise we reset it to 1. At each step we update best to hold the largest run found so far.

Let us trace arr = [1, 3, 5, 4, 7]. The run 1, 3, 5 grows to length 3, then 4 breaks the trend and resets to 1, then 7 extends it back to 2. The best length is 3, matching the expected answer. For a fully sorted array like [1, 2, 3, 4], the run never breaks and we get 4. For a descending array like [5, 4, 3, 2, 1], every step resets, so the answer stays 1.

Equal values also break the run, since the problem requires strictly increasing segments. This is why [1, 1, 1] returns 1.

The time complexity is O(n) because we scan the array once, and the space complexity is O(1) since we only keep two running values.

Best Answers

java
class Solution {
    public int calculate_orchid_height(int[] nums) {
        if (nums.length == 0) return 0;
        int best = 1, cur = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i-1]) { cur++; if (cur > best) best = cur; }
            else cur = 1;
        }
        return best;
    }
}