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: Mutual Preference ProblemPerson 0Prefers: 1,2,3Partner: 1Wants: 2Person 2Prefers: 3,1,0Partner: 3Wants: 0MutualPreferencePerson 1Partner: 0HappyPerson 3Partner: 2HappyPerson 0: UNHAPPYPrefers 2 over partner 1Person 2: UNHAPPYPrefers 0 over partner 3Result: 2 unhappy friendsTwo people found mutually preferred alternatives to their current partners
Understanding the Visualization
1
Input
Preference lists for each person and current pair assignments
2
Process
Check if anyone prefers someone else who also prefers them back
3
Output
Count of people who are unhappy with current pairing
Key Takeaway
🎯 Key Insight: A person is unhappy only when there exists mutual preference - they prefer someone who also prefers them back over current partners
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