Minimum product of k integers in an array of positive Integers Last Updated : 05 May, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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 = 2Output: 9348Explanation: 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 = 3Output: 6Explanation: 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) SpaceTo 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. C++ #include <bits/stdc++.h> using namespace std; int minProduct(vector<int> &arr, int k) { // sort the array sort(arr.begin(), arr.end()); // to store the result int res = 1; // find product of first k elements for(int i = 0; i < k; i++) { res *= arr[i]; } return res; } int main() { vector<int> arr = { 198, 76, 544, 123, 154, 675 }; int k = 2; cout << minProduct(arr, k); return 0; } Java import java.util.Arrays; public class Main { public static int minProduct(int[] arr, int n, int k) { Arrays.sort(arr); long result = 1; for (int i = 0; i < k; i++) { result = (arr[i] * result); } return (int)result; } public static void main(String[] args) { int[] arr = { 198, 76, 544, 123, 154, 675 }; int k = 2; int n = arr.length; System.out.println("Minimum product is " + minProduct(arr, n, k)); } } // This code is contributed by adityamaharshi21. Python # Python program to find minimum product of # k elements in an array def minProduct(arr, n, k): arr.sort() result = 1 for i in range(k): result = (arr[i] * result) return result # Driver code arr = [198, 76, 544, 123, 154, 675] k = 2 n = len(arr) print("Minimum product is", minProduct(arr, n, k)) # This code is contributed by aadityamaharshi21. C# // C# program to find minimum product of // k elements in an array using System; using System.Collections.Generic; public class GFG { public static long minProduct(int[] arr, int n, int k) { Array.Sort(arr); long result = 1; for(int i = 0; i<k; i++){ result = ((long)arr[i] * result); } return result; } // Driver Code public static void Main(String[] args) { int []arr = {198, 76, 544, 123, 154, 675}; int k = 2; int n = arr.Length; Console.Write("Minimum product is " + minProduct(arr, n, k)); } } // This code is contributed by Yash Agarwal(yashagarwal2852002) JavaScript // JavaScript program to find minimum product of // k elements in an array function minProduct(arr, n, k) { arr.sort((a, b) => a - b); let result = 1; for (let i = 0; i < k; i++) { result = (arr[i] * result); } return result; } // Driver code let arr = [198, 76, 544, 123, 154, 675]; let k = 2; let n = arr.length; console.log(`Minimum product is ${minProduct(arr, n, k)}`); // This code is contributed by adityamaharshi21. Output9348[Expected Approach] - Using Max Heap - O(n * log k) Time and O(k) SpaceThe 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. C++ #include <iostream> #include <vector> #include <queue> using namespace std; int minProduct(vector<int>& arr, int k) { // Step 1: Create a max heap with first k elements priority_queue<int> maxHeap; for (int i = 0; i < k; ++i) { maxHeap.push(arr[i]); } // Step 2: Process remaining elements for (int i = k; i < arr.size(); ++i) { if (arr[i] < maxHeap.top()) { maxHeap.pop(); maxHeap.push(arr[i]); } } // Step 3: Multiply elements in heap int product = 1; while (!maxHeap.empty()) { product *= maxHeap.top(); maxHeap.pop(); } return product; } int main() { vector<int> arr1 = {198, 76, 544, 123, 154, 675}; cout << minProduct(arr1, 2) << endl; // Output: 9348 vector<int> arr2 = {5, 4, 1, 2, 3}; cout << minProduct(arr2, 3) << endl; // Output: 6 return 0; } Java import java.util.PriorityQueue; import java.util.Arrays; public class Main { public static int minProduct(int[] arr, int k) { // Step 1: Create a max heap with first k elements PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); for (int i = 0; i < k; ++i) { maxHeap.add(arr[i]); } // Step 2: Process remaining elements for (int i = k; i < arr.length; ++i) { if (arr[i] < maxHeap.peek()) { maxHeap.poll(); maxHeap.add(arr[i]); } } // Step 3: Multiply elements in heap int product = 1; while (!maxHeap.isEmpty()) { product *= maxHeap.poll(); } return product; } public static void main(String[] args) { int[] arr1 = {198, 76, 544, 123, 154, 675}; System.out.println(minProduct(arr1, 2)); // Output: 9348 int[] arr2 = {5, 4, 1, 2, 3}; System.out.println(minProduct(arr2, 3)); // Output: 6 } } Python import heapq def min_product(arr, k): # Step 1: Create a max heap with first k elements max_heap = [-x for x in arr[:k]] heapq.heapify(max_heap) # Step 2: Process remaining elements for i in arr[k:]: if -i > max_heap[0]: heapq.heappop(max_heap) heapq.heappush(max_heap, -i) # Step 3: Multiply elements in heap product = 1 while max_heap: product *= -heapq.heappop(max_heap) return product arr1 = [198, 76, 544, 123, 154, 675] print(min_product(arr1, 2)) # Output: 9348 arr2 = [5, 4, 1, 2, 3] print(min_product(arr2, 3)) # Output: 6 C# using System; using System.Collections.Generic; class Program { public static int MinProduct(int[] arr, int k) { // Step 1: Create a max heap with first k elements SortedSet<int> maxHeap = new SortedSet<int>(Comparer<int>.Create((a, b) => b.CompareTo(a))); for (int i = 0; i < k; ++i) { maxHeap.Add(arr[i]); } // Step 2: Process remaining elements for (int i = k; i < arr.Length; ++i) { if (arr[i] < maxHeap.Max) { maxHeap.Remove(maxHeap.Max); maxHeap.Add(arr[i]); } } // Step 3: Multiply elements in heap int product = 1; foreach (var num in maxHeap) { product *= num; } return product; } static void Main() { int[] arr1 = {198, 76, 544, 123, 154, 675}; Console.WriteLine(MinProduct(arr1, 2)); // Output: 9348 int[] arr2 = {5, 4, 1, 2, 3}; Console.WriteLine(MinProduct(arr2, 3)); // Output: 6 } } JavaScript function minProduct(arr, k) { // Step 1: Create a max heap with first k elements const maxHeap = []; for (let i = 0; i < k; ++i) { maxHeap.push(arr[i]); } maxHeap.sort((a, b) => b - a); // Step 2: Process remaining elements for (let i = k; i < arr.length; ++i) { if (arr[i] < maxHeap[0]) { maxHeap.shift(); maxHeap.push(arr[i]); maxHeap.sort((a, b) => b - a); } } // Step 3: Multiply elements in heap let product = 1; for (const num of maxHeap) { product *= num; } return product; } const arr1 = [198, 76, 544, 123, 154, 675]; console.log(minProduct(arr1, 2)); // Output: 9348 const arr2 = [5, 4, 1, 2, 3]; console.log(minProduct(arr2, 3)); // Output: 6 Output9348 Comment More infoAdvertise with us Next Article Minimum product of k integers in an array of positive Integers G Gitanjali Improve Article Tags : Misc Sorting Heap DSA Arrays Order-Statistics +2 More Practice Tags : ArraysHeapMiscSorting Similar Reads Heap Data Structure A Heap is a complete binary tree data structure that satisfies the heap property: for every node, the value of its children is greater than or equal to its own value. Heaps are usually used to implement priority queues, where the smallest (or largest) element is always at the root of the tree.Basics 2 min read Introduction to Heap - Data Structure and Algorithm Tutorials A Heap is a special tree-based data structure with the following properties:It is a complete binary tree (all levels are fully filled except possibly the last, which is filled from left to right).It satisfies either the max-heap property (every parent node is greater than or equal to its children) o 15+ min read Binary Heap A Binary Heap is a complete binary tree that stores data efficiently, allowing quick access to the maximum or minimum element, depending on the type of heap. It can either be a Min Heap or a Max Heap. In a Min Heap, the key at the root must be the smallest among all the keys in the heap, and this pr 13 min read Advantages and Disadvantages of Heap Advantages of Heap Data StructureTime Efficient: Heaps have an average time complexity of O(log n) for inserting and deleting elements, making them efficient for large datasets. We can convert any array to a heap in O(n) time. The most important thing is, we can get the min or max in O(1) timeSpace 2 min read Time Complexity of building a heap Consider the following algorithm for building a Heap of an input array A. A quick look over the above implementation suggests that the running time is O(n * lg(n)) since each call to Heapify costs O(lg(n)) and Build-Heap makes O(n) such calls. This upper bound, though correct, is not asymptotically 2 min read Applications of Heap Data Structure Heap Data Structure is generally taught with Heapsort. Heapsort algorithm has limited uses because Quicksort is better in practice. Nevertheless, the Heap data structure itself is enormously used. Priority Queues: Heaps are commonly used to implement priority queues, where elements with higher prior 2 min read Comparison between Heap and Tree What is Heap? A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Types of Heap Data Structure: Generally, Heaps can be of two types: Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of its children. The sa 3 min read When building a Heap, is the structure of Heap unique? What is Heap? A heap is a tree based data structure where the tree is a complete binary tree that maintains the property that either the children of a node are less than itself (max heap) or the children are greater than the node (min heap). Properties of Heap: Structural Property: This property sta 4 min read Some other type of HeapBinomial HeapThe main application of Binary Heap is to implement a priority queue. Binomial Heap is an extension of Binary Heap that provides faster union or merge operation with other operations provided by Binary Heap. A Binomial Heap is a collection of Binomial Trees What is a Binomial Tree? A Binomial Tree o 15 min read Fibonacci Heap | Set 1 (Introduction)INTRODUCTION:A Fibonacci heap is a data structure used for implementing priority queues. It is a type of heap data structure, but with several improvements over the traditional binary heap and binomial heap data structures.The key advantage of a Fibonacci heap over other heap data structures is its 5 min read Leftist Tree / Leftist HeapINTRODUCTION:A leftist tree, also known as a leftist heap, is a type of binary heap data structure used for implementing priority queues. Like other heap data structures, it is a complete binary tree, meaning that all levels are fully filled except possibly the last level, which is filled from left 15+ min read K-ary HeapPrerequisite - Binary Heap K-ary heaps are a generalization of binary heap(K=2) in which each node have K children instead of 2. Just like binary heap, it follows two properties: Nearly complete binary tree, with all levels having maximum number of nodes except the last, which is filled in left to r 15 min read Easy problems on HeapCheck if a given Binary Tree is a HeapGiven a binary tree, check if it has heap property or not, Binary tree needs to fulfil the following two conditions for being a heap: It should be a complete tree (i.e. Every level of the tree, except possibly the last, is completely filled, and all nodes are as far left as possible.).Every nodeâs v 15+ min read How to check if a given array represents a Binary Heap?Given an array, how to check if the given array represents a Binary Max-Heap.Examples: Input: arr[] = {90, 15, 10, 7, 12, 2} Output: True The given array represents below tree 90 / \ 15 10 / \ / 7 12 2 The tree follows max-heap property as every node is greater than all of its descendants. Input: ar 11 min read Iterative HeapSortHeapSort is a comparison-based sorting technique where we first build Max Heap and then swap the root element with the last element (size times) and maintains the heap property each time to finally make it sorted. Examples: Input : 10 20 15 17 9 21 Output : 9 10 15 17 20 21 Input: 12 11 13 5 6 7 15 11 min read Find k largest elements in an arrayGiven an array arr[] and an integer k, the task is to find k largest elements in the given array. Elements in the output array should be in decreasing order.Examples:Input: [1, 23, 12, 9, 30, 2, 50], k = 3Output: [50, 30, 23]Input: [11, 5, 12, 9, 44, 17, 2], k = 2Output: [44, 17]Table of Content[Nai 15+ min read Kâth Smallest Element in Unsorted ArrayGiven an array arr[] of N distinct elements and a number K, where K is smaller than the size of the array. Find the K'th smallest element in the given array. Examples:Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3 Output: 7Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 4 Output: 10 Table of Content[Naive Ap 15 min read Height of a complete binary tree (or Heap) with N nodesConsider a Binary Heap of size N. We need to find the height of it. Examples: Input : N = 6 Output : 2 () / \ () () / \ / () () () Input : N = 9 Output : 3 () / \ () () / \ / \ () () () () / \ () ()Recommended PracticeHeight of HeapTry It! Let the size of the heap be N and the height be h. If we tak 3 min read Heap Sort for decreasing order using min heapGiven an array of elements, sort the array in decreasing order using min heap. Examples: Input : arr[] = {5, 3, 10, 1}Output : arr[] = {10, 5, 3, 1}Input : arr[] = {1, 50, 100, 25}Output : arr[] = {100, 50, 25, 1}Prerequisite: Heap sort using min heap.Using Min Heap Implementation - O(n Log n) Time 11 min read Like