Back to November Back to Year 2026
HARD 29 Nov 2026 View on LeetCode

2076. Process Restricted Friend Requests

</> Solution

/**
 * Problem: LeetCode 2076 - Process Restricted Friend Requests
 * Difficulty: Hard
 * Language: Java
 * Author: CodeByTushu
 */

class Solution {
    private int[] parent;

    private int find(int x) {
        while (parent[x] != x) {
            parent[x] = parent[parent[x]]; // path compression
            x = parent[x];
        }
        return x;
    }

    public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {
        parent = new int[n];
        for (int i = 0; i < n; i++) parent[i] = i;

        boolean[] result = new boolean[requests.length];

        for (int i = 0; i < requests.length; i++) {
            int pu = find(requests[i][0]);
            int pv = find(requests[i][1]);

            // Pehle se same group mein hain toh request successful
            if (pu == pv) {
                result[i] = true;
                continue;
            }

            // Check karo ki pu aur pv ko jodne se koi restriction violate toh nahi hoti
            boolean ok = true;
            for (int[] r : restrictions) {
                int px = find(r[0]);
                int py = find(r[1]);
                if ((px == pu && py == pv) || (px == pv && py == pu)) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                parent[pu] = pv; // dono groups merge
                result[i] = true;
            }
        }

        return result;
    }
}

TIME COMPLEXITY

O(M * R * α(N))

SPACE COMPLEXITY

O(N + M)

TOPICS

Game Theory Graph Number Theory Union Find