JAVA || Easy Solution || 100% Faster Code||
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\nPLEASE UPVOTE IF YOU LIKE\n```\n\n\nclass Solution {\n    public int makeConnected(int n, int[][] connections) {\n       // if there are not enough cables than return -1 as  there should be minimum n-1 edges to connect n nodes\n        if(connections.length < n-1)\n            return -1;\n        \n        int[] parent = new int[n];\n        int[] rank = new int[n];\n        \n        for(int i = 0; i<n; i++){\n            parent[i] = i;\n        }\n        Arrays.fill(rank, 1);\n        \n        for(int i = 0; i< connections.length; i++){\n            union(parent, rank, connections[i][0], connections[i][1]);\n        }\n        \n        int countOfUnconnectedComputers =0;\n        for(int i = 0; i< parent.length; i++){\n            if(parent[i] == i)\n                countOfUnconnectedComputers++;\n            \n        } \n        countOfUnconnectedComputers--; // since one root will be there\n        \n        return countOfUnconnectedComputers;\n        \n        \n    }\n    \n    private void union(int[] parent, int[] rank, int x, int y){\n        int parentX = find(parent, x);\n        int parentY = find(parent, y);\n        \n        if(parentX==parentY)\n            return;\n        \n        if(rank[parentX] > rank[parentY]){\n            parent[parentY] = parent[parentX];\n        }else if(rank[parentX] < rank[parentY]){\n             parent[parentX] = parent[parentY];\n        }else{\n            parent[parentY] = parent[parentX];\n            rank[parentX]++;\n        }\n        \n        return;\n\n    }\n    \n    private int find(int[] parent, int x){\n        if(parent[x] ==x)\n            return x;\n        \n        return parent[x] = find(parent, parent[x]);\n    }\n}\n```
['Java']