BudiBadu Logo

Aurora Feather Lights

Array Easy 0 views
Like14

Deep in the Borealis Strait, the sky lanes are guided by shimmering aurora displays. Your task in Aurora Feather Lights is to calculate the total number of illuminated feathers after several geometric "sweeps." Each sweep follows a recursive pattern: a new guiding feather is added, and the entire previous display is replicated four times through mirrored paths across the sky!

The "secret sauce" here is an Exponential Recurrence formula: f(n) = 1 + 4 × f(n-1), where the base case starts at 1. This means the display grows at a staggering rate as the number of sweeps increases. Your solution needs to handle non-negative inputs efficiently to project exactly how many "feather lights" will be in the sky. It’s essential for timing caravans and ensuring the safety of travelers under the northern lights.

This challenge is a great way to practice recursive thinking and exponential growth. It turns a beautiful celestial display into a precise mathematical model that’s both fast and elegant to calculate for sky lane safety!

Examples

Example 1
Input
sweeps = 4
Output
341
Explanation

Four sweeps follow the rule, producing three hundred forty-one shimmering feathers.

Example 2
Input
sweeps = 0
Output
1
Explanation

Only the opening feather lantern glows.

Example 3
Input
sweeps = 2
Output
21
Explanation

The second sweep adds one feather and echoes the earlier light four times.

Algorithm Flow

Recommendation Algorithm Flow for Aurora Feather Lights - Budibadu
Recommendation Algorithm Flow for Aurora Feather Lights - Budibadu

Best Answers

java
import java.util.*;

class Solution {
    public int[][] find_maximal_balanced_subarrays(int[] nums) {
        int p0 = 0, p1 = 0, p2 = 0;
        Map<String, List<Integer>> diffs = new HashMap<>();
        diffs.computeIfAbsent("0,0", k -> new ArrayList<>()).add(-1);
        
        for (int i = 0; i < nums.length; i++) {
            int r = (nums[i] % 3 + 3) % 3;
            if (r == 0) p0++;
            else if (r == 1) p1++;
            else p2++;
            String key = (p0 - p1) + "," + (p1 - p2);
            diffs.computeIfAbsent(key, k -> new ArrayList<>()).add(i);
        }
        
        List<int[]> balanced = new ArrayList<>();
        for (List<Integer> indices : diffs.values()) {
            for (int a = 0; a < indices.size(); a++) {
                for (int b = a + 1; b < indices.size(); b++) {
                    balanced.add(new int[]{indices.get(a) + 1, indices.get(b)});
                }
            }
        }
        
        List<int[]> maximal = new ArrayList<>();
        for (int[] b1 : balanced) {
            boolean isSub = false;
            for (int[] b2 : balanced) {
                if (b2[0] <= b1[0] && b2[1] >= b1[1] && (b2[0] != b1[0] || b2[1] != b1[1])) {
                    isSub = true;
                    break;
                }
            }
            if (!isSub) maximal.add(b1);
        }
        
        maximal.sort(Comparator.comparingInt(a -> a[0]));
        int[][] result = new int[maximal.size()][];
        for (int i = 0; i < maximal.size(); i++) {
            int start = maximal.get(i)[0];
            int end = maximal.get(i)[1];
            result[i] = Arrays.copyOfRange(nums, start, end + 1);
        }
        return result;
    }
}