Count Unhappy Friends - Problem

You are given a list of preferences for n friends, where n is always even.

For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1.

All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi.

However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but:

  • x prefers u over y, and
  • u prefers x over v.

Return the number of unhappy friends.

Input & Output

Example 1 — Basic Unhappiness
$ Input: preferences = [[1,2,3],[3,2,0],[3,1,0],[1,2,0]], pairs = [[0,1],[2,3]]
Output: 2
💡 Note: Person 0 is paired with 1 but prefers 2, and person 2 prefers 0 over their partner 3. Similarly, person 2 is unhappy for the same mutual preference. So 2 people are unhappy.
Example 2 — Everyone Happy
$ Input: preferences = [[1,3,2],[2,3,0],[1,3,0],[0,2,1]], pairs = [[0,1],[2,3]]
Output: 0
💡 Note: No one can find someone they prefer more who also prefers them back over their current partner. Everyone is happy with their current pairing.
Example 3 — Multiple Pairs
$ Input: preferences = [[1,3,2,5,4],[2,3,0,4,5],[3,0,1,4,5],[2,1,4,0,5],[2,1,0,3,5],[3,1,0,2,4]], pairs = [[0,1],[2,3],[4,5]]
Output: 4
💡 Note: With 6 people in 3 pairs, multiple people find mutually preferred alternatives to their current partners, resulting in 4 unhappy friends.

Constraints

  • 2 ≤ n ≤ 500
  • n is even
  • preferences.length == n
  • preferences[i].length == n - 1
  • 0 ≤ preferences[i][j] ≤ n - 1
  • preferences[i] does not contain i
  • All values in preferences[i] are unique
  • pairs.length == n/2
  • pairs[i].length == 2
  • 0 ≤ xi, yi ≤ n - 1
  • xi ≠ yi
  • All pairs contain different people

Visualization

Tap to expand
Count Unhappy Friends INPUT preferences[]: 0 [1,2,3] 1 [3,2,0] 2 [3,1,0] 3 [1,2,0] pairs: 0 1 2 3 Higher preference = lower index Person 0: prefers 1 most Person 1: prefers 3 most preferences=[[1,2,3],[3,2,0], [3,1,0],[1,2,0]] pairs=[[0,1],[2,3]] ALGORITHM STEPS 1 Build Preference Map rank[i][j] = priority of j for i rank[0]: {1:0, 2:1, 3:2} rank[1]: {3:0, 2:1, 0:2} rank[2]: {3:0, 1:1, 0:2} rank[3]: {1:0, 2:1, 0:2} 2 Build Partner Map partner[0]=1, partner[1]=0 partner[2]=3, partner[3]=2 3 Check Unhappy Condition For each x, find u where: x prefers u over partner[x] u prefers x over partner[u] 4 Evaluate Each Person Person 1: paired with 0 prefers 3 over 0 (rank 0 vs 2) 3 prefers 1 over 2 (rank 0 vs 1) --> Person 1 is UNHAPPY Person 3: paired with 2 prefers 1 over 2 (rank 0 vs 1) 1 prefers 3 over 0 (rank 0 vs 2) --> Person 3 is UNHAPPY FINAL RESULT Friend Status: 0 OK 1 SAD 2 OK 3 SAD Why they are unhappy: Person 1: Paired with 0, but prefers 3 Person 3 also prefers 1 over their partner (2) Person 3: Paired with 2, but prefers 1 Person 1 also prefers 3 over their partner (0) Output: 2 Key Insight: The preference map optimization allows O(1) comparison of preferences instead of O(n) linear search. For each person x paired with y, we check all others u (paired with v): if rank[x][u] < rank[x][y] AND rank[u][x] < rank[u][v], then x is unhappy. Time complexity: O(n^2), Space: O(n^2). TutorialsPoint - Count Unhappy Friends | Optimized with Preference Maps
Asked in
Google 15 Facebook 12 Amazon 8
28.5K Views
Medium Frequency
~25 min Avg. Time
890 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