[Java] Failed to pass one test case

Hello. I have the following Java solution for the following question. I passed 8 out of 9 test cases but couldn't pass the last 9th test case. Unfortunately, I can't reveal the name of the company because it is a small finance company and they emphasisedto not disclose the questions.

I would appreciate so much if you can help me deduce what the matter was.

Thank you very much.
image
image
image

class Result {

    /*
     * Complete the 'connectedSum' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER n
     *  2. STRING_ARRAY edges
     */

    public static int connectedSum(int n, List<String> edges) {
        Map<Integer, Set<Integer>> adjList = new HashMap<>();
        
        int total = 0;
        for (int i = 1; i <= n; i++) {
            adjList.put(i, new HashSet<>());
        }
        for (String str : edges) {
            int idx = str.indexOf(' ');
            int from = Integer.parseInt(str.substring(0, idx));
            int to = Integer.parseInt(str.substring(idx + 1));
            adjList.get(from).add(to);
            adjList.get(to).add(from);
        }
        boolean[] visited = new boolean[n+1];
        for (int i = 1; i <= n; i++) {
            if (!visited[i]) {
                int componentSum = traverse(i, visited, adjList);
                total += Math.ceil(Math.sqrt(componentSum));
            }
        }

        return total;
    }
    
    private static int traverse(int vertex, boolean[] visited, Map<Integer, Set<Integer>> adjList) {
        int sum = 1;
        visited[vertex] = true;
        for (Integer next : adjList.get(vertex)) {
            if (!visited[next]) {
                sum += traverse(next, visited, adjList);
            }
        }
        return sum;
    }
}
Comments (4)
No comments yet.