Paths in Maze That Lead to Same Room - Problem
The Maze Confusion Challenge

You're tasked with analyzing a mysterious maze to determine just how confusing it is! The maze consists of n rooms numbered from 1 to n, connected by bidirectional corridors.

The confusion score of the maze is defined as the number of different triangular cycles (cycles of exactly length 3) present in the maze. Think of it this way: if you can start from a room, visit exactly two other rooms, and return to your starting point, you've found a confusing triangle!

For example, the path 1 → 2 → 3 → 1 forms a triangle and contributes to the confusion score. However, longer paths like 1 → 2 → 3 → 4 or paths that backtrack like 1 → 2 → 3 → 2 → 1 don't count.

Your Goal: Given the corridor connections, calculate how many unique triangular cycles exist in the maze.

Input & Output

example_1.py — Basic Triangle
$ Input: n = 4, corridors = [[1,2],[2,3],[3,4],[4,1]]
Output: 0
💡 Note: This forms a square (cycle of length 4) but no triangles. To have a triangle, we'd need a diagonal connection like [1,3] or [2,4].
example_2.py — Single Triangle
$ Input: n = 5, corridors = [[1,2],[2,3],[3,1],[3,4],[4,5]]
Output: 1
💡 Note: Rooms 1, 2, and 3 form exactly one triangle: 1→2→3→1. Rooms 4 and 5 are connected to the triangle but don't form additional triangles.
example_3.py — Multiple Triangles
$ Input: n = 4, corridors = [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Output: 4
💡 Note: This is a complete graph with 4 nodes. Every combination of 3 nodes forms a triangle: (1,2,3), (1,2,4), (1,3,4), (2,3,4).

Visualization

Tap to expand
AliceBobCarolDaveEveSelected EdgeMutual Friends🔍 Algorithm Steps:1. Select Alice-Bob friendship2. Alice's friends: [Carol, Dave]3. Bob's friends: [Dave, Eve]4. Mutual: [Dave] → 1 triangle✓ Triangle: Alice-Bob-Dave💡 Key Insight:Each edge + common neighborcreates exactly one triangle⚡ No duplicates counted⚡ No triangles missed⚡ Optimal efficiency
Understanding the Visualization
1
Map the Network
Build adjacency lists showing each person's friends
2
Pick a Friendship
Select any friendship (edge) between two people
3
Find Mutual Friends
Identify people who are friends with both endpoints
4
Count Triangles
Each mutual friend creates exactly one triangle with the original pair
Key Takeaway
🎯 Key Insight: Instead of checking all possible triangles (O(n³)), we examine each edge and count its common neighbors (O(m√m)). This transforms a cubic problem into a much more efficient graph traversal!

Time & Space Complexity

Time Complexity
⏱️
O(m * √m)

For each edge, we find common neighbors. In sparse graphs, this is much better than O(n³)

n
2n
Linear Growth
Space Complexity
O(n + m)

Space for adjacency lists where m is number of edges

n
2n
Linearithmic Space

Constraints

  • 2 ≤ n ≤ 1000
  • 0 ≤ corridors.length ≤ 5 * 104
  • corridors[i].length == 2
  • 1 ≤ room1i, room2i ≤ n
  • room1i ≠ room2i
  • No duplicate corridors will be given
  • The corridors are undirected (bidirectional)
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 21
31.5K Views
Medium Frequency
~25 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen