Given an array of n positive integers. We are required to write a program to print the minimum product of k integers of the given array.
Examples:
Input : 198 76 544 123 154 675
k = 2
Output : 9348
We get minimum product after multiplying
76 and 123.
Input : 11 8 5 7 5 100
k = 4
Output : 1400
The idea is simple, we find the smallest k elements and print multiplication of them. In below implementation, we have used simple Heap based approach where we insert array elements into a min heap and then find product of top k elements.
C++
// CPP program to find minimum product of // k elements in an array #include <bits/stdc++.h> using namespace std; int minProduct(int arr[], int n, int k) { priority_queue<int, vector<int>, greater<int> > pq; for (int i = 0; i < n; i++) pq.push(arr[i]); int count = 0, ans = 1; // One by one extract items from max heap while (pq.empty() == false && count < k) { ans = ans * pq.top(); pq.pop(); count++; } return ans; } // Driver code int main() { int arr[] = {198, 76, 544, 123, 154, 675}; int k = 2; int n = sizeof(arr) / sizeof(arr[0]); cout << "Minimum product is " << minProduct(arr, n, k); return 0; } |
Java
// Java program to find minimum product of // k elements in an array import java.util.PriorityQueue; class GFG { public static int minProduct(int[] arr, int n, int k) { PriorityQueue<Integer> pq = new PriorityQueue<>(); for (int i = 0; i < n; i++) pq.add(arr[i]); int count = 0, ans = 1; // One by one extract items while(pq.isEmpty() == false && count < k) { ans = ans * pq.element(); pq.remove(); count++; } return ans; } // Driver Code public static void main(String[] args) { int arr[] = {198, 76, 544, 123, 154, 675}; int k = 2; int n = arr.length; System.out.print("Minimum product is " + minProduct(arr, n, k)); } } // This code is contributed by sanjeev2552 |
Python3
# Python3 program to find minimum # product of k elements in an array import math import heapq def minProduct(arr, n, k): heapq.heapify(arr) count = 0 ans = 1 # One by one extract # items from min heap while ( arr ) and count < k: x = heapq.heappop(arr) ans = ans * x count = count + 1 return ans; # Driver method arr = [198, 76, 544, 123, 154, 675] k = 2n = len(arr) print ("Minimum product is", minProduct(arr, n, k)) |
Output:
Minimum product is 9348
Time Complexity : O(n * log n)
Note that the above problem can be solved in O(n) time using methods discussed here and here.
This article is contributed by Gitanjali Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum product pair an array of positive Integers
- Only integer with positive value in positive negative value in array
- Count sequences of positive integers having product X
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Count pairs in Array whose product is a Kth power of any positive integer
- Count of index pairs in array whose range product is a positive integer
- Longest sequence of positive integers in an array
- Maximize product of array by replacing array elements with its sum or product with element from another array
- Number of Subarrays with positive product
- Number of subsequences with positive product
- Length of longest subarray with positive product
- Number of arrays of size N whose elements are positive integers and sum is K
- Longest alternating subsequence in terms of positive and negative integers
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Minimum positive integer required to split the array equally
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Minimum number of changes such that elements are first Negative and then Positive
- Find a pair with maximum product in array of Integers
- Find minimum value to assign all array elements so that array product becomes greater
- Find the smallest positive number missing from an unsorted array | Set 1
Improved By : sanjeev2552

