LeetCode 2709: Greatest Common Divisor Traversal Solution

Master LeetCode problem 2709 (Greatest Common Divisor Traversal), a hard 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.

2709. Greatest Common Divisor Traversal

Problem Explanation

Explanation

To solve this problem, we can construct a graph where each index in the array nums represents a node and there is a directed edge between nodes i and j if gcd(nums[i], nums[j]) > 1. We then need to check if this graph is strongly connected, meaning that there is a path from every node to every other node.

We can use Depth First Search (DFS) to traverse the graph and check if all nodes can be visited from any starting node. If all nodes can be visited, then the answer is true; otherwise, it is false.

Time Complexity: O(n * sqrt(m)), where n is the number of elements in nums and m is the maximum element in nums. Space Complexity: O(n)

Solution Code

import java.util.*;

class Solution {
    public boolean canTraverse(int[] nums) {
        int n = nums.length;
        Map<Integer, List<Integer>> graph = new HashMap<>();
        for (int i = 0; i < n; i++) {
            graph.put(i, new ArrayList<>());
            for (int j = 0; j < n; j++) {
                if (i != j && gcd(nums[i], nums[j]) > 1) {
                    graph.get(i).add(j);
                }
            }
        }
        
        boolean[] visited = new boolean[n];
        
        for (int i = 0; i < n; i++) {
            if (dfs(i, graph, visited)) {
                return true;
            }
        }
        
        return false;
    }
    
    private boolean dfs(int node, Map<Integer, List<Integer>> graph, boolean[] visited) {
        if (visited[node]) {
            return false;
        }
        
        visited[node] = true;
        
        for (int neighbor : graph.get(node)) {
            if (!visited[neighbor] && dfs(neighbor, graph, visited)) {
                return true;
            }
        }
        
        visited[node] = false;
        
        return false;
    }
    
    private int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2709 (Greatest Common Divisor Traversal)?

This page provides optimized solutions for LeetCode problem 2709 (Greatest Common Divisor Traversal) 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 2709 (Greatest Common Divisor Traversal)?

The time complexity for LeetCode 2709 (Greatest Common Divisor Traversal) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2709 on DevExCode?

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

Back to LeetCode Solutions