Suppose there are two friends and now they want to test their friendship that how much compatible they are. Given the numbers n numbered from 1 to n and they are asked to rank the numbers. The task is find the compatibility difference between them. Compatibility difference is the number of mis-matches in the relative ranking of the same movie given by them.
Example 1:
Input : N=5
        a1[] = {3, 1, 2, 4, 5}
        a2[] = {3, 2, 4, 1, 5}
Output : 2
Explanation : Compatibility difference is two
              because first ranks movie 1 before
              2 and 4 but other ranks it after.
Example 2:
 
Input : N=5
        a1[] = {5, 3, 1, 2, 4}
        a2[] = {3, 1, 2, 4, 5}
Output: 5
Explanation: Total difference is four due to
             mis-match in position of 5.
Ã¢â¬â¹
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function findDifference() that takes array A1,array A2 and integer N as parameters and returns the compatibility difference.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3









def findDifference(a1,a2,n):

    #Your code here



Solutions:
def findDifference(a1, a2, n):
	c = 0
	for i in range(n):
		j = i + 1
		while a1[i] != a2[i]:
			c += 1
			(a1[i], a1[j]) = (a1[j], a1[i])
			j += 1
	return c


def findDifference(a1, a2, n):
	count = 0
	for i in range(n):
		j = i + 1
		while a1[i] != a2[i]:
			count += 1
			temp = a1[j]
			a1[j] = a1[i]
			a1[i] = temp
			j += 1
	return count


def findDifference(a1, a2, n):
	cnt = 0
	j = 0
	for i in range(n):
		j = i + 1
		while a1[i] != a2[i]:
			cnt += 1
			tmp = a1[j]
			a1[j] = a1[i]
			a1[i] = tmp
			j += 1
	return cnt


def findDifference(a1, a2, n):
	k = 0
	for i in range(n):
		if a1[i] == a2[i]:
			continue
		else:
			j = i + 1
			while a1[i] != a2[i]:
				k += 1
				(a1[i], a1[j]) = (a1[j], a1[i])
				j += 1
	return k