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

For each person i,&nbsp;preferences[i]&nbsp;contains&nbsp;a list of friends&nbsp;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.&nbsp;Friends in&nbsp;each list are&nbsp;denoted by integers from 0 to n-1.

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

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


	x&nbsp;prefers u&nbsp;over y,&nbsp;and
	u&nbsp;prefers x&nbsp;over v.


Return the number of unhappy friends.

&nbsp;
Example 1:

Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
Output: 2
Explanation:
Friend 1 is unhappy because:
- 1 is paired with 0 but prefers 3 over 0, and
- 3 prefers 1 over 2.
Friend 3 is unhappy because:
- 3 is paired with 2 but prefers 1 over 2, and
- 1 prefers 3 over 0.
Friends 0 and 2 are happy.


Example 2:

Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]]
Output: 0
Explanation: Both friends 0 and 1 are happy.


Example 3:

Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]]
Output: 4


&nbsp;
Constraints:


	2 &lt;= n &lt;= 500
	n&nbsp;is even.
	preferences.length&nbsp;== n
	preferences[i].length&nbsp;== n - 1
	0 &lt;= preferences[i][j] &lt;= n - 1
	preferences[i]&nbsp;does not contain i.
	All values in&nbsp;preferences[i]&nbsp;are unique.
	pairs.length&nbsp;== n/2
	pairs[i].length&nbsp;== 2
	xi != yi
	0 &lt;= xi, yi&nbsp;&lt;= n - 1
	Each person is contained in exactly one pair.


class Solution:
    def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
        dd = {}
        
        for i,x in pairs:
            dd[i] = preferences[i][:preferences[i].index(x)]
            dd[x] = preferences[x][:preferences[x].index(i)]
        
        ans = 0
            
        for i in dd:
            for x in dd[i]:
                if i in dd[x]:
                    ans += 1
                    break
        
        return ans
class Solution {
  public int unhappyFriends(int n, int[][] preferences, int[][] pairs) {
    int[][] rankings = new int[n][n]; // smaller the value, higher the preference
    int[] pairedWith = new int[n];
    for (int i = 0; i < n; i++) {
      for (int rank = 0; rank < n - 1; rank++) {
        int j = preferences[i][rank];
        rankings[i][j] = rank; // person "i" views person "j" with rank
      }
    }
    int unhappy = 0;
    for (int[] pair : pairs) {
      int a = pair[0], b = pair[1];
      pairedWith[a] = b;
      pairedWith[b] = a;
    }
    for (int a = 0; a < n; a++) {
      // "a" prefers someone else
      if (rankings[a][pairedWith[a]] != 0) {
        for (int b = 0; b < n; b++) {
          // "b" prefers to be with "a" over their current partner
          // "a" prefers to be with "b" over their current partner
          if (b != a
              && rankings[b][a] < rankings[b][pairedWith[b]]
              && rankings[a][b] < rankings[a][pairedWith[a]]) {
            unhappy++;
            break;
          }
        }
      }
    }
    return unhappy;
  }
}
class Solution {
public:
    bool check(int x , int y , int u , int v ,int n , vector<vector<int>>& pref){
        int id_x = 0 , id_y = 0 , id_u = 0  , id_v = 0 ;
        //check indices of (y and u) in pref[x] ;
        for(int i = 0 ; i < n - 1; ++i ){
            if(pref[x][i] == y) id_y = i ;
            if(pref[x][i] == u) id_u = i ;
        }
        
        //check indices of (v and x) in pref[u] ;
        for(int i = 0 ; i < n - 1 ; ++i ){
            if(pref[u][i] == v) id_v = i ;
            if(pref[u][i] == x) id_x = i ;
        }
        
        return (id_x < id_v and id_u < id_y) ;
    }
    int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
        unordered_set<int> unhappy ;
        for(int i = 0 ; i < pairs.size() ; ++i ){
            for(int j = i + 1; j < pairs.size() ; ++j){
                int x = pairs[i][0] , y = pairs[i][1] , u = pairs[j][0] , v = pairs[j][1] ;
                // x prefers u over y and u preferes x over v 
                if(check(x,y,u,v,n,preferences)) unhappy.insert(x), unhappy.insert(u) ;
                // x prefers v over y and v prefers x over u 
                if(check(x,y,v,u,n,preferences)) unhappy.insert(x) , unhappy.insert(v) ;
                // y prefers u over x and u prefers y over v
                if(check(y,x,u,v,n,preferences)) unhappy.insert(y) , unhappy.insert(u) ;
                // y prefers v over x and v prefers y over u 
                if(check(y,x,v,u,n,preferences)) unhappy.insert(y) , unhappy.insert(v) ;
            }
        }
        return unhappy.size() ;
    }
};
var unhappyFriends = function(n, preferences, pairs) {
    let happyMap = new Array(n);
    for (let [i, j] of pairs) {
        happyMap[i] = preferences[i].indexOf(j);
        happyMap[j] = preferences[j].indexOf(i);
    }
    
    let unhappy = 0;
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < happyMap[i]; j++) {
            let partner = preferences[i][j];
            if (preferences[partner].indexOf(i) < happyMap[partner]) {
                unhappy++;
                break;
            }
        }
    }
    
    return unhappy;
};
Count Unhappy Friends