You will be given two arrays A and B of positive integers. The number of values in both the arrays will be the same say N. Your task is to find the maximum sum of products of their elements. Each element in A has to be multiplied with exactly one element in B and vice versa such that each element of both the arrays appears exactly once and the sum of product produced is maximum. 
if A = {5,1,3,4,2} and B = {8,10,9,7,6} then a possible sum of product is 5*6 + 1*7 + 3*9 + 4*10 + 2*8.
Example 1:
Input:
N = 3
A[] = {1, 2, 3}
B[] = {4, 5, 1}
Output: 24
Explanation: 5*3 + 4*2 + 1*1 = 24
Example 2:
Input:
N = 2
A[] = {1, 2}
B[] = {4, 4}
Output: 12
Explanation: 4*2 + 4*1 = 12
Your Task:  
You don't need to read input or print anything. Your task is to complete the function MaxSum() which takes the array A[], B[], and N as inputs and returns the answer.
Expected Time Complexity: O(N * log N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i], B[i] ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Mathematics

Starter code: #User function Template for python3

class Solution:

    def MaxSum(self, A, B, N):
        # code here

Solutions:
class Solution:

	def MaxSum(self, A, B, N):
		a = sorted(A)
		b = sorted(B)
		l = []
		for i in range(N):
			l.append(a[i] * b[i])
		return sum(l)


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		m = 0
		for i in range(N):
			m = m + A[i] * B[i]
		return m


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		maxproduct = 0
		for i in range(len(A)):
			maxproduct += A[i] * B[i]
		return maxproduct


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		return sum((a * b for (a, b) in zip(A, B)))


class Solution:

	def MaxSum(self, A, B, N):
		a = sorted(A)
		b = sorted(B)
		c = 0
		for i in range(N):
			c += a[i] * b[i]
		return c


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		s = 0
		for i in range(N - 1, -1, -1):
			s += A[i] * B[i]
		return s


class Solution:

	def MaxSum(self, A, B, N):
		A = sorted(A)
		B = sorted(B)
		s = 0
		for i in range(N):
			s += A[i] * B[i]
		return s


class Solution:

	def MaxSum(self, A, B, N):
		A.sort(reverse=True)
		B.sort(reverse=True)
		s = 0
		for i in range(len(A)):
			p = A[i] * B[i]
			s = s + p
		return s


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		lst = []
		for i in range(N):
			C = A[i] * B[i]
			lst.append(C)
		return sum(lst)


class Solution:

	def MaxSum(self, a, b, n):
		a.sort()
		b.sort()
		total = 0
		for i in range(n - 1, -1, -1):
			total += a[i] * b[i]
		return total


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		return sum((A[i] * B[i] for i in range(N)))


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		sum = 0
		for (i, j) in zip(A, B):
			sum += i * j
		return sum


class Solution:

	def MaxSum(self, A, B, N):
		fact = 0
		A.sort()
		B.sort()
		for i in range(len(A) - 1, -1, -1):
			fact = fact + A[i] * B[i]
		return fact


class Solution:

	def MaxSum(self, A, B, N):
		mu = []
		sum1 = 0
		nA = list(A)
		nA = sorted(nA)
		nB = list(B)
		nB = sorted(nB)
		for i in range(N):
			mu.append(nA[i] * nB[i])
		for a in mu:
			sum1 = sum1 + a
		return sum1


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		ans = 0
		for i in range(N):
			c = A[i] * B[i]
			ans = ans + c
		return ans


class Solution:

	def MaxSum(self, a, b, n):
		a.sort()
		b.sort()
		a = a[::-1]
		b = b[::-1]
		for i in range(len(a)):
			a[i] = a[i] * b[i]
		return sum(a)


class Solution:

	def MaxSum(self, a, b, n):
		a.sort()
		b.sort()
		l = []
		for i in range(n):
			l.append(a[i] * b[i])
		return sum(l)


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		z = 0
		for x in range(N):
			z = z + A[x] * B[x]
		return z


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		sorted_A = A
		sorted_B = B
		sum = 0
		for i in range(N):
			sum += sorted_A[i] * sorted_B[i]
		return sum


class Solution:

	def MaxSum(self, A, B, N):
		e = []
		A.sort()
		B.sort()
		for i in range(len(A)):
			e.append(A[i] * B[i])
		return sum(e)


class Solution:

	def MaxSum(self, a, b, n):
		a.sort()
		b.sort()
		s = 0
		for i in range(n):
			s = s + a[i] * b[i]
		return s


class Solution:

	def MaxSum(self, A, B, N):
		Sum = 0
		A = sorted(A, reverse=True)
		B = sorted(B, reverse=True)
		for i in range(0, N):
			Sum = Sum + A[i] * B[i]
		return Sum


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		summ = 0
		for i in range(0, N):
			val = A[i] * B[i]
			summ = summ + val
		return summ


class Solution:

	def MaxSum(self, A, B, N):
		A.sort(reverse=True)
		B.sort(reverse=True)
		products = []
		for (num1, num2) in zip(A, B):
			products.append(num1 * num2)
		return sum(products)


class Solution:

	def MaxSum(self, A, B, N):
		A.sort()
		B.sort()
		su = 0
		for i in range(N):
			su = su + A[i] * B[i]
		return su
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = [int(x) for x in input().split()]
		B = [int(x) for x in input().split()]
		solObj = Solution()
		print(solObj.MaxSum(A, B, N))


class Solution:

	def MaxSum(self, A, B, N):
		if N == 0:
			return 0
		A.sort()
		B.sort()
		summ = 0
		for (a, b) in zip(A, B):
			summ = summ + a * b
		return summ
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = [int(x) for x in input().split()]
		B = [int(x) for x in input().split()]
		solObj = Solution()
		print(solObj.MaxSum(A, B, N))


class Solution:

	def MaxSum(self, A, B, N):
		r = 0
		A.sort()
		B.sort(reverse=True)
		for i in range(N):
			r += A[i] * B[N - i - 1]
		return r
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = [int(x) for x in input().split()]
		B = [int(x) for x in input().split()]
		solObj = Solution()
		print(solObj.MaxSum(A, B, N))


class Solution:

	def MaxSum(self, A, B, N):
		p = 0
		A.sort(reverse=False)
		B.sort(reverse=False)
		for i in range(len(A)):
			p = p + A[i] * B[i]
		return p
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = [int(x) for x in input().split()]
		B = [int(x) for x in input().split()]
		solObj = Solution()
		print(solObj.MaxSum(A, B, N))