You are conductor of a bus .There are n chairs and n passengers in the bus. You are given an array chairs of length n, where chairs[i] is the position of the i^{th} chair. You are also given the array passengers of length n, where passengers[j] is the position of the j^{th} passenger.
You may perform the following move any number of times:
Increase or decrease the position of the i^{th} passenger by 1 (i.e., moving the i^{th} passenger from position x to x + 1 or x - 1)
Return the minimum number of moves required to move each passenger to a chair such that no two passengers are in the same chair and every passenger gets a chair.
Note that there may be multiple chairs or passengers in the same position at the beginning.
Example 1:
Input: 
chairs = [3,1,5] 
passengers = [2,7,4]
Output: 4
Explanation: 
The passengers are moved as follows:
- The first passenger is moved from position 2 to position 1 using 1 move.
- The second passenger is moved from position 7 to position 5 using 2 moves.
- The third passenger is moved from position 4 to position 3 using 1 move.
In total, 1 + 2 + 1 = 4 moves were used.
Example 2:
Input: 
chairs = [2,2,6,6] 
passengers = [1,3,2,6]
Output: 4
Explanation: 
Note that there are two chairs at position 2 and two chairs at position 6.
The passangers are moved as follows:
- The first passenger is moved from position 1 to position 2 using 1 move.
- The second passenger is moved from position 3 to position 6 using 3 moves.
- The third passenger is not moved.
- The fourth passenger is not moved.
In total, 1 + 3 + 0 + 0 = 4 moves were used.
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMoves() which take length of array, and two arrays chairs and passengers as input, and return an interger value as described above.
Expected Time Complexity: O(nlogn)
Expected Auxiliary Space: O(1)
Constraints:
   n == chairs.length == passengers.length
   1 <= n <= 10^5
   1 <= chairs[i], passengers[j] <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Data structures

Starter code: #User function Template for python3



class Solution:

    def findMoves(self,n,chairs,passengers):

        #code here

Solutions:
class Solution:

	def findMoves(self, n, chairs, passengers):
		sumi = 0
		chairs.sort()
		passengers.sort()
		for (i, j) in zip(chairs, passengers):
			sumi += abs(i - j)
		return sumi


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		count = 0
		for i in range(n):
			count += abs(chairs[i] - passengers[i])
		return count


class Solution:

	def findMoves(self, n, chairs, passengers):
		return sum([abs(x - y) for (x, y) in zip(sorted(chairs), sorted(passengers))])


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		ans = 0
		passengers.sort()
		for i in range(n):
			if chairs[i] == passengers[i]:
				continue
			else:
				ans += max(chairs[i], passengers[i]) - min(chairs[i], passengers[i])
		return ans


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		moves = 0
		for i in range(n):
			diff = abs(chairs[i] - passengers[i])
			moves += diff
		return moves


class Solution:

	def findMoves(self, n, chairs, passengers):
		s = 0
		chairs.sort()
		passengers.sort()
		for i in range(len(passengers)):
			s += abs(chairs[i] - passengers[i])
		return s


class Solution:

	def findMoves(self, n, chairs, passengers):
		m = 0
		chairs.sort()
		c = chairs
		passengers.sort()
		p = passengers
		for i in range(n):
			m += abs(c[i] - p[i])
		return m


from collections import Counter

class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		distances = 0
		while passengers:
			distances += abs(chairs.pop() - passengers.pop())
		return distances


class Solution:

	def findMoves(self, n, a, b):
		a.sort()
		b.sort()
		c = 0
		for i in range(n):
			c += abs(a[i] - b[i])
		return c


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		return sum((abs(i - j) for (i, j) in zip(chairs, passengers)))


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		return sum((abs(e - t) for (e, t) in zip(chairs, passengers)))


class Solution:

	def findMoves(self, n, chair, pas):
		chair.sort()
		pas.sort()
		ans = 0
		for i in range(n):
			ans += abs(chair[i] - pas[i])
		return ans


class Solution:

	def findMoves(self, n, chairs, passengers):
		count = 0
		chairs = sorted(chairs)
		passengers = sorted(passengers)
		for i in range(len(chairs)):
			count += abs(chairs[i] - passengers[i])
		return count


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		return sum((abs(a - b) for (a, b) in zip(chairs, passengers)))


class Solution:

	def findMoves(self, n, chairs, passen):
		chairs.sort()
		passen.sort()
		res = 0
		for i in range(n):
			res += abs(passen[i] - chairs[i])
		return res


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		p = set(chairs)
		k = list(p)
		m = 0
		for i in range(len(chairs)):
			m = m + abs(chairs[i] - passengers[i])
		return m


class Solution:

	def findMoves(self, n, chairs, passengers):
		ls = []
		chairs.sort()
		passengers.sort()
		i = 0
		j = 0
		while i < len(chairs) and j < len(passengers):
			y = abs(chairs[i] - passengers[j])
			ls.append(y)
			i += 1
			j += 1
		return sum(ls)


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		occupied = [False] * n
		ret = 0
		for i in range(n):
			ret += abs(passengers[i] - chairs[i])
		return ret


class Solution:

	def findMoves(self, n, c, p):
		c.sort()
		p.sort()
		s = 0
		for i in range(n):
			s += abs(c[i] - p[i])
		return s
		return abs(sum(c) - sum(p))


class Solution:

	def findMoves(self, n, c, p):
		c.sort()
		p.sort()
		m = 0
		for x in range(n):
			while p[x] != c[x]:
				if p[x] > c[x]:
					p[x] -= 1
					m += 1
				else:
					p[x] += 1
					m += 1
		return m


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		l = []
		for i in range(len(chairs)):
			a = chairs[i] - passengers[i]
			if a >= 0:
				l.append(a)
			else:
				l.append(-a)
		return sum(l)


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		return sum((abs(chairs[i] - passengers[i]) for i in range(len(chairs))))


class Solution:

	def findMoves(self, n, chairs, passengers):
		d = 0
		s_chairs = sorted(chairs)
		s_passengers = sorted(passengers)
		for move in range(0, n):
			d += abs(s_chairs[move] - s_passengers[move])
		return abs(d)


class Solution:

	def findMoves(self, n, chair, people):
		m = 0
		chair.sort()
		c = chair
		people.sort()
		p = people
		for i in range(n):
			m = m + abs(c[i] - p[i])
		return m


class Solution:

	def findMoves(self, n, chairs, passengers):
		chairs.sort()
		passengers.sort()
		j = 0
		d = 0
		for i in chairs:
			d += abs(i - passengers[j])
			j += 1
		return d


class Solution:

	def findMoves(self, n, chairs, passengers):
		k = 0
		c = sorted(chairs)
		p = sorted(passengers)
		for i in range(n):
			k += abs(c[i] - p[i])
		return k


class Solution:

	def findMoves(self, n, ch, ps):
		ch.sort()
		ps.sort()
		(i, j) = (0, 0)
		ans = 0
		while i < len(ch) and j < len(ps):
			ans += abs(ch[i] - ps[j])
			i += 1
			j += 1
		return ans


class Solution:

	def findMoves(self, n, c, p):
		c.sort()
		p.sort()
		ans = 0
		for i in range(n):
			ans += abs(c[i] - p[i])
		return ans


class Solution:

	def findMoves(self, n, chairs, passengers):
		ch = sorted(chairs)
		ps = sorted(passengers)
		arr = []
		for i in range(n):
			x = ch[i] - ps[i]
			if x < 0:
				x = x * -1
			arr.append(x)
		return sum(arr)