![]() |
VOOZH | about |
Given an array arr[] containing n positive integers and an integer k. Your task is to find the minimum possible product of k elements of the given array.
Examples:
Input: arr[] = [198, 76, 544, 123, 154, 675], k = 2
Output: 9348
Explanation: We will choose two smallest numbers from the given array to get the minimum product. The two smallest numbers are 76 and 123, and their product is 9348.Input: arr[] = [5, 4, 1, 2, 3], k = 3
Output: 6
Explanation: We will choose three smallest numbers from the given array to get the minimum product. The three smallest numbers are 1, 2, and 3, and their product is 6.
To get the minimum product of k elements, we are required to choose the k smallest elements. We sort the given array in ascending order, and find the product of first k elements of the sorted array.
9348
The idea is to use max heap (priority queue) to find the k smallest elements of the given array.
1) Create a max heap and push first k array elements into it.
2) For remaining n-k elements, compare each element with the heap top, if smaller, then remove root of the heap and insert into the heap. If greater than ignore.
3) Finally compute product of the heap elements.
9348