DEV Community

Cover image for 2076. Process Restricted Friend Requests
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

2076. Process Restricted Friend Requests

2076. Process Restricted Friend Requests

Difficulty: Hard

Topics: Senior Staff, Union-Find, Graph Theory, Weekly Contest 267

You are given an integer n indicating the number of people in a network. Each person is labeled from 0 to n - 1.

You are also given a 0-indexed 2D integer array restrictions, where restrictions[i] = [xᵢ, yᵢ] means that person xᵢ and person yᵢ cannot become friends, either directly or indirectly through other people.

Initially, no one is friends with each other. You are given a list of friend requests as a 0-indexed 2D integer array requests, where requests[j] = [uⱼ, vⱼ] is a friend request between person uⱼ and person vⱼ.

A friend request is successful if uⱼ and vⱼ can be friends. Each friend request is processed in the given order (i.e., requests[j] occurs before requests[j + 1]), and upon a successful request, uⱼ and vⱼ become direct friends for all future friend requests.

Return a boolean array result, where each result[j] is true if the jᵗʰ friend request is successful or false if it is not.

Note: If uⱼ and vⱼ are already direct friends, the request is still successful.

Example 1:

  • Input: n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]]
  • Output: [true,false]
  • Explanation:
    • Request 0: Person 0 and person 2 can be friends, so they become direct friends.
    • Request 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (1--2--0).

Example 2:

  • Input: n = 3, restrictions = [[0,1]], requests = [[1,2],[0,2]]
  • Output: [true,false]
  • Explanation:
    • Request 0: Person 1 and person 2 can be friends, so they become direct friends.
    • Request 1: Person 0 and person 2 cannot be friends since person 0 and person 1 would be indirect friends (0--2--1).

Example 3:

  • Input: n = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]]
  • Output: [true,false,true,false]
  • Explanation:

    • Request 0: Person 0 and person 4 can be friends, so they become direct friends.
    • Request 1: Person 1 and person 2 cannot be friends since they are directly restricted.
    • Request 2: Person 3 and person 1 can be friends, so they become direct friends.
    • Request 3: Person 3 and person 4 cannot be friends since person 0 and person 1 would be indirect friends (0--4--3--1). Example 4:
  • Input: n = 2, restrictions = [], requests = [[0,1]]

  • Output: [true]

Example 5:

  • Input: n = 2, restrictions = [[0,1]], requests = [[0,1]]
  • Output: [false]

Example 6:

  • Input: n = 4, restrictions = [[0,1]], requests = [[1,2],[0,2],[2,3]]
  • Output: [true,false,true]

Example 7:

  • Input: n = 6, restrictions = [[0,1],[2,3]], requests = [[0,2],[0,3],[1,2],[1,3]]
  • Output: [true,true,false,false]

Example 8:

  • Input: n = 3, restrictions = [[0,1]], requests = [[0,2],[0,2]]
  • Output: [true,true]

Example 9:

  • Input: n = 4, restrictions = [[0,1],[2,3]], requests = [[1,2],[0,3]]
  • Output: [true,false]

Example 10:

  • Input: n = 5, restrictions = [[0,1],[3,4]], requests = [[0,2],[2,3],[1,4],[1,3]]
  • Output: [true,true,false,false]

Constraints:

  • 2 <= n <= 1000
  • 0 <= restrictions.length <= 1000
  • restrictions[i].length == 2
  • 0 <= xᵢ, yᵢ <= n - 1
  • xᵢ != yᵢ
  • 1 <= requests.length <= 1000
  • requests[j].length == 2
  • 0 <= uⱼ, vⱼ <= n - 1
  • uⱼ != vⱼ

Hint:

  1. For each request, we could loop through all restrictions. Can you think of doing a check-in close to O(1)?
  2. Could you use Union Find?

Similar Questions:

  1. 305. Number of Islands II
  2. 1202. Smallest String With Swaps
  3. 2127. Maximum Employees to Be Invited to a Meeting

Solution:

We use a Union-Find (Disjoint Set Union) data structure to track friend groups as requests are processed sequentially. For each request [u, v], we check whether merging the two components would violate any restriction. If the two endpoints of a restriction would land in the same component after the merge, we reject the request; otherwise we union them and accept. Union-Find keeps the "is u connected to v" check nearly O(1), while restrictions are scanned only at merge time.

Approach

  • Initialize a Union-Find with n elements, each being its own component (parent[i] = i), using union by rank and path compression.
  • Maintain the restrictions list as a flat list of [x, y] pairs — no preprocessing needed.
  • For each request [u, v] in order:
    • Find the component roots ru = find(u), rv = find(v).
    • If ru == rv, the two people are already (directly or indirectly) friends → mark request successful, skip merge.
    • Otherwise, iterate over every restriction [x, y]:
    • Compute rx = find(x) and ry = find(y).
    • If one restricted person lives in ru and the other in rv, merging would make them indirect friends → reject the request.
    • If no restriction blocks the merge, union(u, v) and mark request successful.
  • Collect the boolean results in an array and return.

Let's implement this solution in PHP: 2076. Process Restricted Friend Requests

<?php
/**
 * @param Integer $n
 * @param Integer[][] $restrictions
 * @param Integer[][] $requests
 * @return Boolean[]
 */
function friendRequests(int $n, array $restrictions, array $requests): array
{
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

// Test cases
echo friendRequests(3, [[0,1]], [[0,2], [2,1]]) .  "\n";                                  // Output: [true,false]
echo friendRequests(3, [[0,1]], [[1,2], [0,2]]) .  "\n";                                  // Output: [true,false]
echo friendRequests(5, [[0,1],[1,2],[2,3]], [[0,4],[1,2],[3,1],[3,4]]) .  "\n";           // Output: [true,false,true,false]
echo friendRequests(2, [], [[0,1]]) .  "\n";                                              // Output: [true]
echo friendRequests(2, [[0,1]], [[0,1]]) .  "\n";                                         // Output: [false]
echo friendRequests(4, [[0,1]], [[1,2],[0,2],[2,3]]) .  "\n";                             // Output: [true,false,true]
echo friendRequests(6, [[0,1],[2,3]], [[0,2],[0,3],[1,2],[1,3]]) .  "\n";                 // Output: [true,true,false,false]
echo friendRequests(3, [[0,1]], [[0,2],[0,2]]) .  "\n";                                   // Output: [true,true]
echo friendRequests(4, [[0,1],[2,3]], [[1,2],[0,3]]) .  "\n";                             // Output: [true,false]
echo friendRequests(5, [[0,1],[3,4]], [[0,2],[2,3],[1,4],[1,3]]) .  "\n";                 // Output: [true,true,false,false]
?>
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Why Union-Find works: Friend requests build a graph incrementally. The only thing that matters for future requests is which connected component a person belongs to, not the specific edges. Union-Find compresses each component to a single root, so "are these two connected?" becomes an O(α(n)) operation.
  • Checking restrictions efficiently: When considering a merge of components ru and rv, we only need to know whether any restriction pair [x, y] has find(x) in one of {ru, rv} and find(y) in the other. This is an O(|restrictions|) scan per request, not per pair.
  • Same-component requests: If u and v are already in the same friend group (e.g., direct friends or connected indirectly), the request is always accepted — no new edges are added that could break restrictions, and merging is a no-op.
  • Path compression: find flattens the tree so subsequent lookups are near-constant, keeping the restriction scan cheap even as components grow.
  • Union by rank: Keeps the tree shallow so find stays close to O(α(n)), bounding the total cost across 1 × 10³ requests.
  • Correctness of rejection: A restriction [x, y] forbids any path between x and y. If after merging ru and rv, find(x) == find(y), then a path exists — so the merge must be rejected. Rejecting keeps all prior invariants intact.
  • Order matters: Because requests are processed strictly in order, we check restrictions against the current component structure each time — the same restriction may block later but not earlier requests.

Complexity Analysis

  • Let R = restrictions.length, Q = requests.length, α(·) = inverse Ackermann (effectively constant).
  • Time Complexity:
    • find / union per operation: O(α(n)).
    • Per request: two find calls (O(α(n))) + up to R restriction scans, each requiring two find calls → O(R · α(n)).
    • Total: O((Q + 1) · R · α(n)) worst case, i.e., O(Q · R · α(n)).
    • With Q, R ≤ 1000 and n ≤ 1000, this is well within limits (~10⁶ operations).
  • Space Complexity: O(n) for parent and rank arrays, plus O(Q) for the result array. Restrictions are stored as-is — O(R).

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
Buy Me A Coffee

If you want more helpful content like this, feel free to follow me:

Top comments (0)