Given an array of N positive integers. You need to write a program to print the minimum product of k integers of the given array.
Note: Since output could be large, hence module 10^9+7 and then print answer.
Example 1:
Input : Arr[] = {1, 2, 3, 4, 5}, K = 2
Output : 2
Explanation:
If we have an array [1, 2, 3, 4, 5]. 
We will get the minimum product after multiplying
1 and 2 that is 2.
So, the answer is 2.
Example 2:
Input : Arr[] = {9, 10, 8}, K = 3
Output : 720
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function minProduct() that takes an array (arr), sizeOfArray (n), element K, and return the minimum product. The driver code takes care of the printing.
Expected Time Complexity: O(nlogn).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ K, N ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Sorting

Data structures

Starter code: #User function Template for python3

class Solution:
    def minProduct(self, a, n, k): 
        # Complete the function
      


Solutions:
class Solution:

	def minProduct(self, a, n, k):
		MOD = 10 ** 9 + 7
		a.sort()
		new_arr = a[:k]
		prod = 1
		for val in new_arr:
			prod *= val
		return prod % MOD


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		p = 1
		for i in range(k):
			p = p * a[i]
		return p % (10 ** 9 + 7)


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		p = 1
		for i in range(0, k):
			p = p * a[i]
		return p % 1000000007


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		num = 1
		for i in range(k):
			num = a[i] * num
		return num % 1000000007


class Solution:

	def minProduct(self, a, n, k):
		M = 1000000007
		a.sort()
		ans = 1
		for i in range(k):
			ans = ans * a[i] % M
		return ans


class Solution:

	def minProduct(self, a, n, k):
		sum = 1
		a = sorted(a)
		for i in range(k):
			sum = sum * a[i]
			sum %= 10 ** 9 + 7
		return sum


class Solution:

	def minProduct(self, a, n, k):
		ans = 1
		a.sort()
		mod = 10 ** 9 + 7
		if n == k:
			for i in a:
				ans *= i
			return ans % mod
		if k == 1:
			return a[0] % mod
		for i in range(0, k):
			ans *= a[i]
		return ans % mod


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		mod = 10 ** 9 + 7
		res = 1
		for x in a[:k]:
			res *= x
			res %= mod
		return res


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		a1 = a[:k]
		p = 1
		for i in a1:
			p = p * i
		return p % 1000000007


class Solution:

	def minProduct(self, a, n, k):
		mod = 10 ** 9 + 7
		a.sort()
		new_arr = a[:k]
		prod = 1
		for i in new_arr:
			prod *= i
		return prod % mod


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		elements = a[:k]
		res = 1
		for i in elements:
			res *= i
		return res % (10 ** 9 + 7)


class Solution:

	def minProduct(self, a, n, k):
		s = sorted(a)
		p = 1
		for i in s[:k]:
			p *= i
		if p > 100000007:
			return p % 1000000007
		return p


class Solution:

	def minProduct(self, a, n, k):
		a = sorted(a)
		p = a[:k]
		c = 1
		for i in p:
			c = c * i
		return c % (10 ** 9 + 7)


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		m = 1000000007
		prod = 1
		k = min(k, n)
		for i in a[:k]:
			prod = prod * i % m
		return prod


class Solution:

	def minProduct(self, a, n, k):
		p = 1
		if k == 0:
			return p
		for i in range(0, k):
			p = p * min(a)
			a.remove(min(a))
		return p % (pow(10, 9) + 7)


import heapq

class Solution:

	def minProduct(self, a, n, k):
		mod = 10 ** 9 + 7
		product = []
		mul = 1
		if k == 0:
			return mul
		for i in range(k):
			heapq.heappush(product, -1 * a[i])
		for i in range(k, n, 1):
			max = -product[0]
			if a[i] < max:
				heapq.heapreplace(product, -1 * a[i])
		while product:
			max = -heapq.heappop(product)
			mul = mul * (max % mod)
		return mul % mod


class Solution:

	def minProduct(self, a, n, k):
		arr.sort()
		m = pow(10, 9) + 7
		ans = 1
		for i in range(k):
			ans *= arr[i]
		return ans % m


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		mod = 10 ** 9 + 7
		count = 1
		count1 = 0
		for i in a:
			count = count * i
			count1 = count1 + 1
			if count1 == k:
				break
		if k > 0:
			return count % mod
		else:
			return 1


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		l = a[:k]
		m = 1
		for i in range(len(l)):
			m = m * l[i]
		return m % (10 ** 9 + 7)


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		n = min(n, k)
		mod = 1000000007
		s = 1
		for i in range(n):
			s = a[i] * s
			s = s % mod
		return s % mod


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		prod = 1
		for i in range(k):
			prod *= a[i]
		return prod % (1000000000 + 7)


class Solution:

	def minProduct(self, a, n, k):
		c = 1
		a = sorted(a)
		for i in range(k):
			c *= a[i]
		return c % 1000000007


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		mul = 1
		for i in range(k):
			mul *= arr[i]
		return mul % 1000000007


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		mod = 1000000007
		p = arr[:k]
		product = 1
		for i in p:
			product *= i
		return product % mod


from functools import reduce

class Solution:

	def partition(self, arr, l, r):
		pivot = arr[r]
		i = l - 1
		for j in range(l, r):
			if arr[j] < pivot:
				i += 1
				(arr[i], arr[j]) = (arr[j], arr[i])
		(arr[i + 1], arr[r]) = (arr[r], arr[i + 1])
		return i + 1

	def quicksort(self, arr, l, r):
		if l < r:
			mid = (l + r) // 2
			p = self.partition(arr, l, r)
			self.quicksort(arr, l, p - 1)
			self.quicksort(arr, p + 1, r)

	def minProduct(self, a, n, k):
		if n > 100000:
			return 1
		self.quicksort(a, 0, n - 1)
		prod = 1
		if k > 0 and n <= 100000:
			for i in range(k):
				prod *= a[i]
			return reduce(lambda x, y: x * y, a[0:k]) % 1000000007
		else:
			return 1


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		m = 1000000007
		product = 1
		for i in range(k):
			product *= a[i]
		return product % m


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		t = 1
		for i in range(k):
			t = t * a[i]
		return int(t % 1000000007)


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		arr = a[:k]
		module = 10 ** 9 + 7
		product = 1
		for item in arr:
			product *= item
		return product % module


class Solution:

	def minProduct(self, a, n, k):
		a.sort()
		resArr = a[:k]
		MOD = 10 ** 9 + 7
		prod = 1
		for elem in resArr:
			prod *= elem
		return prod % MOD


class Solution:

	def minProduct(self, a, n, k):
		l = list(a)
		l.sort()
		x = []
		s = 1
		z = 10 ** 9 + 7
		for i in range(k):
			x.append(l[i])
		for i in x:
			s = s * i
		return s % z


class Solution:

	def minProduct(self, a, n, k):
		arr.sort()
		ar = arr[:k]
		p = 1
		for j in ar:
			p *= j
		return p % (10 ** 9 + 7)


class Solution:

	def minProduct(self, a, n, k):
		c = 1
		b = 1
		d = k
		mod = 1000000007
		a.sort()
		for x in range(n):
			if n == k:
				c *= a[x]
			elif x < k:
				b *= a[x]
				b = b % mod
		if n == k:
			return c % mod
		else:
			return b % mod


import math

class Solution:

	def minProduct(self, a, n, k):
		s = 1
		for i in sorted(a)[:k]:
			s *= i
		return s % (10 ** 9 + 7)