LeetCode 2316: Count Unreachable Pairs of Nodes in an Undirected Graph Solution

Master LeetCode problem 2316 (Count Unreachable Pairs of Nodes in an Undirected Graph), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2316. Count Unreachable Pairs of Nodes in an Undirected Graph

Problem Explanation

Explanation

To solve this problem, we can first create an adjacency list to represent the graph. Then, we can perform a Depth First Search (DFS) from each node to mark all reachable nodes. Finally, we can calculate the number of unreachable pairs of nodes by subtracting the count of reachable pairs from the total possible pairs of nodes.

  1. Create an adjacency list from the given edges array.
  2. Perform a Depth First Search (DFS) from each node to mark all reachable nodes.
  3. Calculate the total possible pairs of nodes using the formula n * (n-1) / 2.
  4. Calculate the count of reachable pairs of nodes by summing up the product of sizes of connected components.
  5. Calculate the number of unreachable pairs by subtracting the count of reachable pairs from the total possible pairs.
  6. Return the number of unreachable pairs.

Time Complexity: O(n + edges), where n is the number of nodes and edges is the number of edges. Space Complexity: O(n + edges) for the adjacency list and recursive stack space.

Solution Code

class Solution {
    public int countUnreachablePairs(int n, int[][] edges) {
        List<List<Integer>> adjList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            adjList.add(new ArrayList<>());
        }
        
        for (int[] edge : edges) {
            adjList.get(edge[0]).add(edge[1]);
            adjList.get(edge[1]).add(edge[0]);
        }
        
        boolean[] visited = new boolean[n];
        int reachablePairs = 0;
        
        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                int componentSize = dfs(i, adjList, visited);
                reachablePairs += componentSize * (componentSize - 1) / 2;
            }
        }
        
        int totalPairs = n * (n - 1) / 2;
        return totalPairs - reachablePairs;
    }
    
    private int dfs(int node, List<List<Integer>> adjList, boolean[] visited) {
        visited[node] = true;
        int size = 1;
        for (int neighbor : adjList.get(node)) {
            if (!visited[neighbor]) {
                size += dfs(neighbor, adjList, visited);
            }
        }
        return size;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2316 (Count Unreachable Pairs of Nodes in an Undirected Graph)?

This page provides optimized solutions for LeetCode problem 2316 (Count Unreachable Pairs of Nodes in an Undirected Graph) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2316 (Count Unreachable Pairs of Nodes in an Undirected Graph)?

The time complexity for LeetCode 2316 (Count Unreachable Pairs of Nodes in an Undirected Graph) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2316 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2316 in Java, C++, or Python.

Back to LeetCode Solutions