Java Union Find O(N)
```\nclass Solution {\n    class UF{\n        int[] parent;\n        public UF(int n){\n            parent = new int[n];;\n            for(int i=0;i<n;++i)\n   
```\nclass Solution {\n    class UF{\n        int[] parent;\n        public UF(int n){\n            parent = new int[n];;\n            for(int i=0;i<n;++i)\n                parent[i] = i;\n        }\n        \n        public int find(int x){\n            while(x != parent[x]){\n                parent[x] =  parent[parent[x]];\n                x = parent[x];\n            }\n            return x;\n        }\n        \n        public boolean union(int x, int y, int[][] rest){\n            int rootX = find(x);\n            int rootY = find(y);\n            \n            if(rootX != rootY){\n                for(int[] res : rest){\n                    int px = find(res[0]), py = find(res[1]);\n                    if((px == rootX && py == rootY) || (px == rootY && py == rootX))\n                           return false;\n                }\n                parent[rootY] = rootX;\n            }\n        \n            return true;\n        }\n    }\n    public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {\n        UF uf = new UF(n);\n        boolean[] res = new boolean[requests.length];\n        int  i = 0;\n        for(int[] req : requests){\n            if(uf.union(req[0], req[1], restrictions))\n                res[i] = true;\n            i++;\n        }\n        return res;\n    }\n}
[]