Java Union Find
-> Connect the connections one by one using Union Find(UF)\n-> If already connected, just count the number of redundant connections.(count)\n-> Also count the d
-> Connect the connections one by one using Union Find(UF)\n-> If already connected, just count the number of redundant connections.(count)\n-> Also count the distict parents in the connected graph(This gives the count of the number of connected components).\n-> Required Answer would be -> (totalComputers-(computersVisited)+(connectedCompnents-1)\n```\n    int[] parent = new int[100000];\n    int[] size = new int[100000];\n\n    public int makeConnected(int n, int[][] connections) {\n\n        for(int i=0;i<100000;i++) {\n            parent[i]=i;\n            size[i]=1;\n        }\n\n        Set<Integer> set = new HashSet<>();\n        int count=0; // redundant connections.\n        for (int[] connection : connections) {\n            if(getParent(connection[0])==getParent(connection[1])) { // Already connected.\n                count++;\n            } else {\n                union(connection[0],connection[1]); // Not connected, we need to join.\n                set.add(connection[0]);\n                set.add(connection[1]);\n            }\n        }\n\n        int size = set.size();\n        int parentCount=0; // Distinct parents after joining, If all components are connected this count would be 1.\n        for (int p : parent) {\n            int pp = getParent(p);\n            if(set.contains(pp)) {\n               parentCount++;\n               set.remove(pp);\n            }\n        }\n\n        int req = n-size+(parentCount-1);\n        return count>=(req)?req:-1;\n\n    }\n\n    private int getParent(int x) {\n        while(x!=parent[x]) {\n            parent[x] = parent[parent[x]];\n            x = parent[x];\n        }\n        return x;\n    }\n    private void union(int a,int b) {\n        int pA = getParent(a);\n        int pB = getParent(b);\n        if(pA!=pB) {\n            if(size[pA]>size[pB]) {\n                parent[pB]=parent[pA];\n                size[pA]+=size[pB];\n            } else {\n                parent[pA]=parent[pB];\n                size[pB]+=size[pA];\n            }\n        }\n    }\n```
[]