Count Unhappy Friends - Problem
The Friend Pairing Dilemma

Imagine you're organizing a social event where n friends (where n is always even) need to be paired up for activities. Each friend has provided you with their preferences list - a ranked ordering of all other friends from most preferred to least preferred partner.

You've already created the pairs based on some criteria, but now you need to determine how many friends are unhappy with their pairing.

A friend x is considered unhappy if they are paired with y, but there exists another friend u (who is paired with v) such that:
• Friend x prefers u over their current partner y
• Friend u also prefers x over their current partner v

Goal: Count the total number of unhappy friends given their preferences and current pairings.

Input & Output

example_1.py — Basic Case
$ Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
Output: 2
💡 Note: Friend 0 is paired with 1 but prefers 2, and friend 2 prefers 0 over 3. Similarly, friend 2 is paired with 3 but prefers 1, and friend 1 prefers 2 over 0. So both friends 0 and 2 are unhappy.
example_2.py — All Happy
$ Input: n = 2, preferences = [[1], [0]], pairs = [[0, 1]]
Output: 0
💡 Note: Both friends are paired with their most preferred choice, so no one is unhappy.
example_3.py — Larger Group
$ Input: n = 6, preferences = [[1, 4, 3, 2, 5], [0, 3, 2, 4, 5], [3, 0, 1, 4, 5], [2, 1, 4, 0, 5], [1, 3, 2, 0, 5], [0, 1, 2, 3, 4]], pairs = [[0, 1], [2, 3], [4, 5]]
Output: 4
💡 Note: Multiple friends have mutual preferences with others outside their current pairing, resulting in 4 unhappy friends.

Visualization

Tap to expand
Count Unhappy Friends Visualization0123Current Pair (0,1)Current Pair (2,3)0 prefers 2 over 12 prefers 1 over 3Analysis ResultsFriend 0: Paired with 1, but prefers 2. Friend 2 also prefers 0 over 3. → Unhappy ❌Friend 1: Paired with 0, checks other options but no mutual better preference. → Happy ✅Friend 2: Paired with 3, but prefers 1. Friend 1 prefers 3 over 0, not 2. → Happy ✅Friend 3: Similar analysis shows no mutual better preferences. → Happy ✅
Understanding the Visualization
1
Current Pairings
Start with the given friend pairs
2
Build Preference Maps
Convert preference lists to ranking maps for fast lookups
3
Check Each Friend
For each person, see if they prefer someone else who also prefers them
4
Count Unhappy Friends
Sum up all friends who have mutual preferences outside their current pairing
Key Takeaway
🎯 Key Insight: Build ranking maps first for O(1) preference comparisons, then check each friend's potential alternative pairings for mutual preferences.

Time & Space Complexity

Time Complexity
⏱️
O(n³)

For each of n friends, we check n other friends, and each check requires O(n) preference list search

n
2n
Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
Linear Space

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 are unique
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
18.5K Views
Medium Frequency
~25 min Avg. Time
847 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