Given an array where each element is the money a person have and there is only Rs. 3 note. We need to check whether it is possible to divide the money equally among all the persons or not. If it is possible then find Minimum number of transactions needed.
 
Example 1:
Input: nums = [5, 8, 11]
Output: 1
Explanation: 3rd person can give Rs. 3
then all person would have Rs. 8
Example 2:
Input: nums = [1, 2, 3, 4]
Output: -1
Explanation: 1 + 2 + 3 + 4 = 10. Rs. 10 
can not be distributed among 4 persons.
 
Your Task:
You don't need to read or print anything. Your task is to complete the function MinimumTransaction() which takes the array as input parameter and returns the minimum number of transactions needed to distribute the money equally. If not possible returns -1.
 
Expected Time Complexity: O(n) where n is length of array
Expected Space Complexity: O(1)
 
Constraints:
1 <= n <= 10^{5}
1 <= elements of array <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY

Starter code: #User function Template for python3

class Solution:
	def MinimumTransaction(self, nums):
		# Code here

Solutions:
class Solution:

	def MinimumTransaction(self, nums):
		s = sum(nums)
		if s % len(nums) != 0:
			return -1
		m = s // len(nums)
		res = 0
		for i in range(len(nums)):
			diff = nums[i] - m
			if diff <= 0:
				continue
			if diff % 3 != 0:
				return -1
			res += diff // 3
		return res


class Solution:

	def MinimumTransaction(self, a):
		n = len(nums)
		s = sum(nums)
		c = 0
		r = s // n
		if s % n != 0:
			return -1
		for i in range(0, n - 1):
			f = abs(a[i] - a[i + 1])
			if f % 3 != 0:
				return -1
		for i in range(0, n):
			g = abs(a[i] - r)
			if g % 3 != 0:
				return -1
			c = c + g // 3
		return c // 2
		return -1


class Solution:

	def MinimumTransaction(self, nums):
		s = sum(nums)
		if s % len(nums) != 0:
			return -1
		each = s // len(nums)
		count = 0
		for i in range(len(nums)):
			ele = nums[i]
			if ele > each:
				k = ele - each
				if k % 3 != 0:
					return -1
				else:
					count += k // 3
			elif each > ele:
				k = each - ele
				if k % 3 != 0:
					return -1
			else:
				continue
		return count


class Solution:

	def MinimumTransaction(self, nums):
		s = sum(nums)
		if s % len(nums) != 0:
			return -1
		each = s // len(nums)
		count = 0
		for i in range(len(nums)):
			ele = nums[i]
			if ele > each:
				k = ele - each
				if k % 3 != 0:
					return -1
				else:
					count += k // 3
			elif each > ele:
				k = each - ele
				if k % 3 != 0:
					return -1
			else:
				continue
		return count
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		nums = list(map(int, input().split()))
		ob = Solution()
		ans = ob.MinimumTransaction(nums)
		print(ans)


class Solution:

	def MinimumTransaction(self, nums):
		s = sum(nums)
		if s % len(nums) == 0:
			b = s // len(nums)
			c = 0
			for i in nums:
				if i < b:
					i = b - i
					if i % 3 == 0:
						c += i // 3
					else:
						return -1
				elif i > b:
					i = i - b
					if i % 3 == 0:
						c += i // 3
					else:
						return -1
			return c // 2
		return -1