BSIO - Google Interview Question help needed

You are given a list of integers of the same length A and B. You are also given a two-dimensional list of integers C where each element is of the form [i, j] which means that you can swap A[i] and A[j] as many times as you want.

Return the maximum number of pairs where A[i] = B[i] after the swapping.


  • n ≤ 100,000 where n is the length of A and B
  • m ≤ 100,000 where m is the length of C

Example 1


A = [1, 2, 3, 4]

B = [2, 1, 4, 3]

C = [ [0, 1], [2, 3] ]




We can swap A[0] with A[1] then A[2] with A[3].

can someone help me with the approach?