Java Solution with Detailed Explanation
class Solution {\n    \n     /Steps to solve the problem: \n        \n        1) Make Adjacency List -> count of Edges(E)\n        2) DFS Traversal -> count of 
class Solution {\n    \n     /*Steps to solve the problem: \n        \n        1) Make Adjacency List -> count of Edges(E)\n        2) DFS Traversal -> count of Components(C)\n        3) n -> count of Nodes(N)\n        4) Redundant Edges: R = E - [(N-1) - (C-1)] <--- important step\n           $ When number of components is 1, formula becomes:\n            R = E - [N-1]\n           -For N nodes N-1 edges are required for complete connection.\n            Rest will be redundant, hence the formula.\n        5) Negative Cases (return -1): \n           - E<N-1  \n           - R<C-1 \n        6) Else return C-1 (This is the minimum number of edges required to connect C components);\n        \n        If you have enough redudant edges R >= C-1, then you can use C-1 edges to connect a network.\n        \n        */\n        \n    public int makeConnected(int n, int[][] connections) {\n        \n\t\t/* Count of Nodes -> N */\n        int N = n;  \n\t\t\n\t\t/* Count of Edges -> E */\n        int E = connections.length; \n\t\t\n        int R = 0;\n        int C = 0;\n        \n        if(E<N-1)\n            return -1;\n        \n\t\t/* Create Adjaceny List */\n        List<List<Integer>> adj = new ArrayList<>();\n        \n          for(int i = 0; i<n; i++)\n             adj.add(new ArrayList<>());\n        \n          for(int[] c : connections){\n                adj.get(c[0]).add(c[1]);\n                adj.get(c[1]).add(c[0]);\n            }\n        \n        boolean[] visited = new boolean[n];\n        \n\t\t/* Perform DFS traversal to count components -> C */\n        for(int i = 0; i<n; i++){\n            if(!visited[i]){\n                dfs(adj,visited,i);\n                C++;\n            }\n        }\n        \n\t\t/* Calculate Redundant Edges -> R */\n        R = E-((N-1)-(C-1));\n\n        if(R<C-1)\n            return -1;\n        \n        return C-1;\n    }\n    \n\t/* DFS Traversal */\n    public void dfs(List<List<Integer>> adj, boolean[] visited, int i){\n        \n        if(visited[i])\n            return;\n        \n        visited[i] = true;\n        \n        List<Integer> connection = adj.get(i);\n        for(int c : connection){\n            if(!visited[c])\n                dfs(adj,visited,c);\n        }\n        return;\n    }\n}
['Depth-First Search', 'Java']