You are given two arrays, A and B, of equal size N. The task is to find the minimum value of A[0] * B[0] + A[1] * B[1] + .... + A[N-1] * B[N-1], where shuffling of elements of arrays A and B is allowed.
Example 1:
Input:
N = 3 
A[] = {3, 1, 1}
B[] = {6, 5, 4}
Output:
23 
Explanation:
1*6+1*5+3*4 = 6+5+12
= 23 is the minimum sum
 
Example 2:
Input:
N = 5
A[] = {6, 1, 9, 5, 4}
B[] = {3, 4, 8, 2, 4}
Output:
80
Explanation:
2*9+3*6+4*5+4*4+8*1
=18+18+20+16+8
= 80 is the minimum sum
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function minValue() which takes the arrays A[], B[] and its size N as inputs and returns the minimum sum.
 
Expected Time Complexity: O(N. log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Data structures

Greedy algorithms

Starter code: #User function Template for python3



class Solution:

    def minValue(self, a, b, n):

        # Your code goes here

        



Solutions:
class Solution:

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


class Solution:

	def minValue(self, a, b, n):
		ans = 0
		mina = min(a)
		minb = min(b)
		if mina > minb:
			a.sort(reverse=True)
			b.sort()
		else:
			a.sort()
			b.sort(reverse=True)
		for i in range(n):
			ans += a[i] * b[i]
		return ans


class Solution:

	def minValue(self, a, b, n):
		a.sort()
		b.sort(reverse=True)
		result = 0
		for i in range(n):
			result += a[i] * b[i]
		return result


class Solution:

	def minValue(self, a, b, n):
		a.sort()
		b.sort()
		b1 = b[::-1]
		s = 0
		for k in range(n):
			s += a[k] * b1[k]
		return s


class Solution:

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


class Solution:

	def minValue(self, a, b, n):
		a.sort()
		b.sort()
		a.reverse()
		s = 0
		r = 0
		while r != len(a):
			s = s + a[r] * b[r]
			r += 1
		return s


class Solution:

	def minValue(self, a, b, n):
		sum = 0
		a.sort()
		b.sort()
		b.reverse()
		for index in range(n):
			sum += a[index] * b[index]
		return sum


class Solution:

	def minValue(self, a, b, n):
		a.sort()
		b.sort()
		b.reverse()
		c = list()
		for i in range(n):
			c.append(a[i] * b[i])
		return sum(c)


class Solution:

	def minValue(self, a, b, n):
		if sum(a) > sum(b):
			a.sort(reverse=True)
			b.sort()
		else:
			b.sort(reverse=True)
			a.sort()
		l = []
		for (i, j) in zip(a, b):
			l.append(i * j)
		return sum(l)


class Solution:

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


class Solution:

	def minValue(self, a, b, n):
		a.sort()
		b.sort(reverse=True)
		return sum([i * j for (i, j) in zip(a, b)])


class Solution:

	def minValue(self, a, b, n):
		a = sorted(a)
		b = sorted(b, reverse=True)
		prod = 0
		for i in range(n):
			prod += a[i] * b[i]
		return prod


class Solution:

	def minValue(self, a, b, n):
		a.sort()
		b.sort()
		b = b[::-1]
		sum = 0
		for (i, j) in zip(a, b):
			sum += i * j
		return sum


class Solution:

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


class Solution:

	def minValue(self, a, b, n):
		start_ptr = 0
		end_ptr = n - 1
		a.sort()
		b.sort()
		res_1 = 0
		res_2 = 0
		for i in range(n):
			res_1 += a[start_ptr] * b[end_ptr]
			res_2 += a[end_ptr] * b[start_ptr]
			start_ptr += 1
			end_ptr -= 1
		return min(res_1, res_2)


class Solution:

	def minValue(self, a, b, n):
		a = sorted(a)
		b = sorted(b)
		i = 0
		j = n - 1
		ans = 0
		while i < n and j >= 0:
			ans = ans + a[i] * b[j]
			i = i + 1
			j = j - 1
		return ans


class Solution:

	def minValue(self, a, b, n):
		a = sorted(a)
		b = sorted(b, reverse=True)
		pro = 0
		pro1 = 1
		for i in range(len(a)):
			pro += a[i] * b[i]
		return pro


class Solution:

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


class Solution:

	def minValue(self, a, b, n):
		c = sorted(a)
		d = sorted(b)
		d.reverse()
		ans = 0
		for i in range(n):
			ans += c[i] * d[i]
		return ans


class Solution:

	def minValue(self, a, b, n):
		min_sum = 0
		a.sort()
		b.sort()
		b.reverse()
		for i in range(n):
			operation = a[i] * b[i]
			min_sum += operation
		return min_sum


class Solution:

	def minValue(self, a, b, n):
		a = sorted(a)
		b = sorted(b)
		b.reverse()
		s = 0
		for i in range(len(a)):
			s = s + a[i] * b[i]
		return s


class Solution:

	def minValue(self, a, b, n):
		a.sort()
		b.sort()
		b.reverse()
		d = len(a)
		sum = 0
		for i in range(d):
			c = a[i] * b[i]
			sum = sum + c
		return sum


import numpy as np

class Solution:

	def minValue(self, a, b, n):
		a.sort()
		b.sort(reverse=True)
		ans = list(np.multiply(a, b))
		return sum(ans)


class Solution:

	def minValue(self, a, b, n):
		res = 0
		aS = sorted(a)
		bS = sorted(b, reverse=True)
		for i in range(n):
			res += aS[i] * bS[i]
		return res


class Solution:

	def minValue(self, A, B, n):
		A = sorted(A)
		B = sorted(B)
		list1 = []
		left = 0
		for i in range(len(B) - 1, -1, -1):
			list1.append(B[i] * A[left])
			left += 1
		return sum(list1)


class Solution:

	def minValue(self, a, b, n):
		c = 0
		arr1 = sorted(a)
		arr2 = sorted(b, reverse=True)
		for i in range(n):
			c += int(arr1[i]) * int(arr2[i])
		return c


class Solution:

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


class Solution:

	def minValue(self, a, b, n):
		sum = 0
		a.sort()
		b.sort(reverse=True)
		for i in range(len(a)):
			temp = a[i] * b[i]
			sum = sum + temp
		return sum


class Solution:

	def minValue(self, a, b, n):
		s = 0
		a = sorted(a)
		b = sorted(b)
		for i in range(n):
			s += a[i] * b[-(i + 1)]
		return s