JavaScript | Clean Union-Find w/ Explanation | 99%
Version W/o Explanation (see below for commented):\n\nconst makeConnected = (n, connections) => {\n  const parent = Array(n).fill(-1); \n  let spare = 0, notCon
Version W/o Explanation (see below for commented):\n```\nconst makeConnected = (n, connections) => {\n  const parent = Array(n).fill(-1); \n  let spare = 0, notConnected = n - 1\n  \n  for (let [node, connection] of connections) {\n    if (find(node) !== find(connection)) {\n      union(node, connection);\n      notConnected--;\n    } else spare++;\n  }\n  \n  return notConnected <= spare ? notConnected : -1;\n  \n  function find(node) {\n    if (parent[node] === -1) return node\n    parent[node] = find(parent[node])\n    return parent[node]\n  }\n  \n  function union(node1, node2) {\n    const parent1 = find(node1), parent2 = find(node2);\n    if (parent1 !== parent2) parent[parent2] = parent1;\n  }\n};\n```\n\nVersion w/ Explanation:\n```\nconst makeConnected = (n, connections) => {\n  //Initialize our UnionFind Array.  Basic intuition is that indexes \n  //stand for nodes, -1 means they aren\'t connected to anything.\n  //As our Union function runs, it will create these connections.\n  //Find will tell us what group nodes belong to.\n  const parent = Array(n).fill(-1); \n  \n  //Spare tracks how many unnecessary connections we have -\n  //If we try to connect two computers already in the same group, \n  //we increment spare.  Not connected tracks how many \n  //Are not in groups. -1 is because one cable connectes two computers.\n  //You need five cables to connect six computers, not six, etc.\n  let spare = 0, notConnected = n - 1\n  \n  //Iterate through node / connections.  If both nodes aren\'t in the same group -\n  //I.e. Are not already connected, we connect them and decrement the not\n  //connected counter.  If we get a connection for two computers that are already\n  //connected, we increment the spare counter.\n  for (let [node, connection] of connections) {\n    if (find(node) !== find(connection)) {\n      union(node, connection);\n      notConnected--;\n    } else spare++;\n  }\n  \n  //If our count of unconnected computers is less than our spares,\n  //We simply return the not connected number.  Else, we can\'t\n  //connect them all and return -1.\n  return notConnected <= spare ? notConnected : -1;\n  \n  //Standard find function.  If node at idx === -1 it belongs\n  //in it\'s own group / to itself.  If not, we dfs to find the parent node.\n  //Parent node is the \'chosen\' one to represent the group.  If we union\n  //1 and 2 for example, we pick either 1 or 2 to be the \'group representative\'.\n  function find(node) {\n    if (parent[node] === -1) return node\n    parent[node] = find(parent[node])\n    return parent[node]\n  }\n  \n  //Standard union.  We check to make sure nodes aren\'t already in the same\n  //group first.  If not, we set parent[parent2] = parent1.  Works the same way\n  //around if we pick parent[parent1] = parent2.  Just need to arbitrarily pick\n  //one to be the \'group representative\'.  That said, there is an optimization for\n  //this algorithm called Union By Rank that will always union smaller groups to \n  //larger ones to reduce dfs look up times.  That\'s not implemented here.\n  function union(node1, node2) {\n    const parent1 = find(node1), parent2 = find(node2);\n    if (parent1 !== parent2) parent[parent2] = parent1;\n  }\n};\n```
['JavaScript']