Easy Way
class Solution {\n    public static int find(int x,int parent[])\n    {\n        if(parent[x]==x)\n            return x;\n        else\n             return find
class Solution {\n    public static int find(int x,int parent[])\n    {\n        if(parent[x]==x)\n            return x;\n        else\n             return find(parent[x],parent);\n    }\n    public static void union(int i,int j,int parent[],int arr[])\n    {\n        int x = find(i,parent);\n        int y = find(j,parent);\n        if(x==y){\n            arr[0]++;\n            return;}\n        else {\n               //System.out.println(i+","+j+","+x+","+y);\n            parent[y]= x;}\n    }\n    public int makeConnected(int n, int[][] connections) {\n        \n        int m= connections.length;\n          if(connections.length < n-1)return -1;\n        int parent[] = new int[n];\n        for(int i=0;i<n;i++)\n            parent[i]=i;\n        \n        int arr[] = new int[1];\n        for(int i=0;i<m;i++)\n        {      \n            int a= connections[i][0];\n            int b= connections[i][1];\n            union(a,b,parent,arr);\n          \n        }\n     int noOfConnectedComponent =-1;\n        for(int i=0;i<n;i++){\n            if(parent[i]==i)\n                noOfConnectedComponent++;\n        }\n        \n        \n        if(arr[0]<noOfConnectedComponent)\n            return -1;\n        else\n            return noOfConnectedComponent;\n    }\n}
['Union Find']