Process Restricted Friend Requests - Problem

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] = [xi, yi] means that person xi and person yi 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] = [uj, vj] is a friend request between person uj and person vj.

A friend request is successful if uj and vj can be friends. Each friend request is processed in the given order, and upon a successful request, uj and vj become direct friends for all future friend requests.

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

Note: If uj and vj are already direct friends, the request is still successful.

Input & Output

Example 1 — Basic Restriction Violation
$ Input: n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1],[0,1]]
Output: [true,false,false]
💡 Note: Request [0,2] is allowed since no restriction. Request [2,1] would connect 0-1 indirectly (through 2), violating restriction. Request [0,1] directly violates restriction.
Example 2 — Already Connected
$ Input: n = 3, restrictions = [[0,1]], requests = [[1,2],[1,2]]
Output: [true,true]
💡 Note: First request [1,2] is allowed. Second identical request is still successful since 1 and 2 are already friends.
Example 3 — Multiple Components
$ Input: n = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]]
Output: [true,false,false,false]
💡 Note: [0,4] allowed. [1,2] violates direct restriction. [3,1] violates restriction [1,2] indirectly. [3,4] would connect restricted pairs through component {0,4}.

Constraints

  • 2 ≤ n ≤ 1000
  • 0 ≤ restrictions.length ≤ 1000
  • restrictions[i].length == 2
  • 0 ≤ xi, yi ≤ n - 1
  • xi ≠ yi
  • 1 ≤ requests.length ≤ 1000
  • requests[j].length == 2
  • 0 ≤ uj, vj ≤ n - 1
  • uj ≠ vj

Visualization

Tap to expand
Process Restricted Friend Requests INPUT Network (n=3 people) 0 1 2 RESTRICTED Restrictions: [[0, 1]] Requests: [0,2] [2,1] [0,1] Request 1 Request 2 Request 3 ALGORITHM STEPS 1 Initialize Union-Find Each person is own parent 0 1 2 2 Request [0,2] Check: 0-2 violates 0-1? No conflict --> Union 0,2 true 3 Request [2,1] Check: 2-1 connects 0-1? Conflict! 2 in set with 0 false 4 Request [0,1] Direct restriction exists Blocked by restriction false Final Groups: 0 2 1 FINAL RESULT Boolean Array: true [0,2] false [2,1] false [0,1] Output: [true,false,false] Only request [0,2] succeeded because it doesn't violate any restrictions. OK - Verified Key Insight: Union-Find tracks friend groups. Before each union, check if merging would connect any restricted pair. For each restriction [x,y], if find(x) would equal find(y) after union, the request must be rejected. Time: O(R * K * alpha(N)) where R=requests, K=restrictions, alpha=inverse Ackermann (nearly constant) TutorialsPoint - Process Restricted Friend Requests | Union-Find with Restriction Validation
Asked in
Google 12 Facebook 8 Amazon 5
28.5K Views
Medium Frequency
~35 min Avg. Time
427 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen