[Java] Union Find - clean code
\nclass Solution {\n    public int makeConnected(int n, int[][] connections) {\n        int[] arr = new int[n];\n        for(int i = 0; i < arr.length; i++) {\n
```\nclass Solution {\n    public int makeConnected(int n, int[][] connections) {\n        int[] arr = new int[n];\n        for(int i = 0; i < arr.length; i++) {\n            arr[i] = i;\n        }\n        \n        int unnecessaryCables = 0;\n        for(int[] con : connections) {\n            int u = con[0], v = con[1];\n            int groupU = findGroup(u, arr), groupV = findGroup(v, arr);\n            if (groupU == groupV) unnecessaryCables++;\n            else {\n                arr[groupU] = groupV;\n                n--;\n            }\n        } \n        \n        return unnecessaryCables < n - 1 ? -1 : n - 1;\n    }\n    \n    private int findGroup(int i, int[] arr) {\n        while(arr[i] != i) {\n            i = arr[i];\n        }\n        return i;\n    } \n}\n\n\nDFS solution\n\nclass Solution {\n    public int makeConnected(int n, int[][] connections) {\n        // 1 number of connections need to connect n computer is n - 1\n        \n        if (connections.length < n - 1) return -1;\n        \n        List<Integer>[] adjacents = new List[n];\n        for(int[] con : connections) {\n            int u = con[0], v = con[1];\n            if (adjacents[u] == null) adjacents[u] = new ArrayList<>();\n            if (adjacents[v] == null) adjacents[v] = new ArrayList<>();\n            adjacents[u].add(v);\n            adjacents[v].add(u);\n        }\n        boolean[] isApartOfAnyNetwork = new boolean[n];\n        int numberOfNetwork = 0;\n        for(int i = 0; i < n; i++) {\n            if (!isApartOfAnyNetwork[i]) {\n                dfs(i, adjacents, isApartOfAnyNetwork);\n                numberOfNetwork++;\n            }\n        }\n        return numberOfNetwork - 1;\n    }\n    \n    private void dfs(int i, List<Integer>[] adjacents, boolean[] isApartOfAnyNetwork) {\n        if (isApartOfAnyNetwork[i]) return;\n        isApartOfAnyNetwork[i] = true;\n        \n        if (adjacents[i] != null) {\n            \n            for(int j : adjacents[i]) {\n                dfs(j, adjacents, isApartOfAnyNetwork);\n            }\n        }\n    }\n}\n```
[]