Code Logo

Mirage Oasis Rings

Published at05 Jan 2026
Array Manipulation Easy 7 views
Like11

You can think of this as a small game with a very specific goal. In Mirage Oasis Rings, you are trying to work toward the right number by following one clear idea.

Calculate mirage oasis ring pattern 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 = 3, the answer is 40. Three rings repeat the pattern, generating forty mirrored arcs. Another example is rings = 0, which gives 1. Only the first water mirror is visible.

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 = 3
Output
40
Explanation

Three rings repeat the pattern, generating forty mirrored arcs.

Example 2
Input
rings = 0
Output
1
Explanation

Only the first water mirror is visible.

Example 3
Input
rings = 5
Output
364
Explanation

Five rings follow the ritual, producing three hundred sixty-four shimmering reflections.

Algorithm Flow

Recommendation Algorithm Flow for Mirage Oasis Rings

Solution Approach

This problem asks us to count the number of distinct rings formed by the connections in a graph. A ring here is a connected group of nodes, so the answer is the number of connected components in the graph, counting isolated nodes as their own components.

The cleanest way to solve this is a graph traversal. We build an undirected adjacency list, then run BFS or DFS from each unvisited node, counting one component each time we start a new traversal.

Here is the implementation:

function count_distinct_rings(n, edges) {
    const adj = Array.from({ length: n }, () => []);
    for (const [u, v] of edges) {
        adj[u].push(v);
        adj[v].push(u);
    }
    const visited = new Array(n).fill(false);
    let count = 0;
    for (let i = 0; i < n; i++) {
        if (!visited[i]) {
            count++;
            const stack = [i];
            visited[i] = true;
            while (stack.length > 0) {
                const u = stack.pop();
                for (const v of adj[u]) {
                    if (!visited[v]) {
                        visited[v] = true;
                        stack.push(v);
                    }
                }
            }
        }
    }
    return count;
}

We build an undirected adjacency list so each edge connects both directions. Then we iterate over every node; whenever we find one not yet visited, we have discovered a new component, so we increment the count and flood-fill that component. Because edges are undirected, a component is fully reachable from any of its nodes.

Let us trace n = 3, edges = [[0, 1], [1, 2]]. Nodes 0, 1, 2 all connect into one component, so the answer is 1. For n = 3, edges = [[0, 1]], nodes 0 and 1 form one component and node 2 is isolated, giving 2. With no edges and n = 1, the single node is one component, giving 1.

The time complexity is O(n + e) and the space complexity is O(n).

Best Answers

java
class Solution {
    public int count_distinct_rings(int n, int[][] conn) {
        java.util.List<Integer>[] g = new java.util.ArrayList[n];
        for (int i = 0; i < n; i++) g[i] = new java.util.ArrayList<>();
        for (int[] e : conn) { g[e[0]].add(e[1]); g[e[1]].add(e[0]); }
        boolean[] seen = new boolean[n];
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (!seen[i]) {
                count++;
                java.util.Stack<Integer> st = new java.util.Stack<>();
                seen[i] = true; st.push(i);
                while (!st.isEmpty()) {
                    int u = st.pop();
                    for (int w : g[u]) if (!seen[w]) { seen[w] = true; st.push(w); }
                }
            }
        }
        return count;
    }
}