VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-product-k-integers-array-positive-integers/

⇱ Minimum product of k integers in an array of positive Integers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum product of k integers in an array of positive Integers

Last Updated : 5 May, 2025

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.

[Naive Approach] - Using Sorting - O(n * log n) Time and O(1) Space

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.


Output
9348

[Expected Approach] - Using Max Heap - O(n * log k) Time and O(k) Space

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.


Output
9348
Comment