Java | Sets with Communities | Easy to Understand
Idea: Create a list of sets of communities (List<Set<Integer>> communities) and check whether can be they joined or not based on restrictions.\n\nAlgorithm: Let
**Idea**: Create a list of sets of communities (`List<Set<Integer>> communities`) and check whether can be they joined or not based on restrictions.\n\n**Algorithm**: Let\'s take example from the problem. `n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]]`. You will firstly create such communities` [ {0}, {1}, {2} ]`. Furthermore, we will maintain restrictions in hashmap for quick access.\nThen, in each request we check whether we are able to merge these communities together or not. After first iteration (`[0,2]`), we will get `[ {0,2}, {1} ]`. In the second request, we see that `{1}` cannot be inside the set with `0` and `2`, becase `2` has `0` in its community. Please, check the code for additional conditions of comparing the sets with each other.\n\n\n```\nclass Solution {\n    public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {\n        boolean[] res = new boolean[requests.length];\n        List<Integer>[] restricted = new List[n];\n        for (int i = 0; i < n; i++)\n            restricted[i] = new ArrayList<Integer>();\n        \n        for (int[] r : restrictions) {\n            restricted[r[0]].add(r[1]);\n            restricted[r[1]].add(r[0]);\n        }\n        \n        List<HashSet<Integer>> communities = new ArrayList();\n        for (int i = 0; i < n; i++) {\n            HashSet<Integer> s = new HashSet<Integer>();\n            s.add(i);\n            communities.add(s);\n        }\n        \n        int k = 0;\n        for (int[] req : requests) {\n            int x = req[0];\n            int y = req[1];\n            int xi = 0, yi = 0; // Indices of sets in which there people belong to\n            \n            for (int i = 0; i < communities.size(); i++) {\n                if (communities.get(i).contains(x)) {\n                    xi = i;\n                }\n                if (communities.get(i).contains(y)) {\n                    yi = i;\n                }\n            }\n            \n            if (xi == yi) {  // they are both already in one community\n                res[k++] = true;\n                continue;\n            }\n            \n            int flag = -1;\n            for (int node : communities.get(xi)) {\n                if (flag >= 0) break;\n                for (int other : communities.get(yi)) {\n                    if (restricted[node].contains(other)) {\n                        flag = 0;\n                        break;\n                    } else if (communities.get(xi).contains(other)) {\n                        flag = 1;\n                        break;\n                    }\n                }\n            }\n            if (flag == 0) {\n                res[k++] = false;\n            } else {\n                mergeSets(communities, xi, yi);\n                res[k++] = true;\n            }\n        } \n        \n        return res;\n    }\n    \n    public void mergeSets(List<HashSet<Integer>> communities, int xi, int yi) {\n        HashSet<Integer> a = communities.get(xi);\n        HashSet<Integer> b = communities.get(yi);\n        if (xi > yi) {\n            communities.remove(xi);\n            communities.remove(yi);\n        } else {\n            communities.remove(yi);\n            communities.remove(xi);\n        }\n        for (int val : b) {\n            a.add(val);\n        }\n        communities.add(a);\n    }\n}\n```
[]