Open In App

Minimum product of k integers in an array of positive Integers

Last Updated : 18 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

Recommended Practice

An approach using Sorting:

  • Sort the array in increasing order.
  • Take the product of the first K elements of the array
  • Return the result.

Below is the implementation of the above approach:

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)
{
    sort(arr, arr + n);
 
    long long result = 1;
 
    for (int i = 0; i < k; i++) {
        result = ((long long)arr[i] * result);
    }
 
    return result;
}
 
// 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;
}
 
// This code is contributed by hkdass001


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.


Output

Minimum product is 9348

Time Complexity: O(n*log(n))
Auxiliary Space: O(1)

An approach using Heap data structure: The idea is simple, we find the smallest k elements and print multiplication of them. In the below implementation, we have used a simple Heap-based approach where we insert array elements into a min-heap and then find product of top k elements.

Flowchart:

Flowchart

Implementation:

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;
            //to hold larger values
            long ans = 1;
 
            // One by one extract items
            while(pq.isEmpty() == false && count < k)
            {
                ans = ans * pq.element()%1000000007;
                pq.remove();
                count++;
            }
         
            return (int)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 = 2
n = len(arr)
print ("Minimum product is",
       minProduct(arr, n, k))


C#




// C# program to find minimum product of
// k elements in an array
 
using System;
using System.Collections.Generic;
public class GFG
{
  public static int minProduct(int[] arr, int n, int k)
  {
    List<int> pq = new List<int>();
    for (int i = 0; i < n; i++)
      pq.Add(arr[i]);
 
    int count = 0, ans = 1;
 
    // One by one extract items
    while(pq.Count!=0 && count < k)
    {
      pq.Sort();
      ans = ans * pq[0];
      pq.RemoveAt(0);
      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;
    Console.Write("Minimum product is " +
                  minProduct(arr, n, k));
  }
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// Javascript program to find minimum product of
// k elements in an array
 
function minProduct(arr, n, k) {
    let pq = new Array();
    for (let i = 0; i < n; i++)
        pq.push(arr[i]);
 
    let count = 0, ans = 1;
 
    // One by one extract items
    while (pq.length != 0 && count < k) {
        pq.sort((a, b) => a - b);
        ans = ans * pq[0];
        pq.shift(0);
        count++;
    }
 
    return ans;
}
 
// Driver Code
 
let arr = [198, 76, 544, 123, 154, 675];
let k = 2;
let n = arr.length;
document.write("Minimum product is " + minProduct(arr, n, k));
 
 
// This code is contributed by Saurabh Jaiswal
</script>


Output

Minimum product is 9348

Time Complexity: O(n * log n), 
Auxiliary Space: O(n) for priority queue

Note: The above problem can be solved in O(n) time using methods discussed here and here.



Previous Article
Next Article

Similar Reads

C++ Program for Minimum product pair an array of positive Integers
Given an array of positive integers. We are required to write a program to print the minimum product of any two numbers of the given array.Examples: Input : 11 8 5 7 5 100 Output : 25 Explanation : The minimum product of any two numbers will be 5 * 5 = 25. Input : 198 76 544 123 154 675 Output : 7448 Explanation : The minimum product of any two num
3 min read
Java Program for Minimum product pair an array of positive Integers
Given an array of positive integers. We are required to write a program to print the minimum product of any two numbers of the given array.Examples: Input : 11 8 5 7 5 100 Output : 25 Explanation : The minimum product of any two numbers will be 5 * 5 = 25. Input : 198 76 544 123 154 675 Output : 9348 Explanation : The minimum product of any two num
4 min read
Minimum product pair an array of positive Integers
Given an array of positive integers. We are required to write a program to print the minimum product of any two numbers of the given array. Examples: Input: 11 8 5 7 5 100Output: 25 Explanation: The minimum product of any two numbers will be 5 * 5 = 25. Input: 198 76 544 123 154 675 Output: 7448Explanation: The minimum product of any two numbers wi
12 min read
Generate an array with K positive numbers such that arr[i] is either -1 or 1 and sum of the array is positive
Given two positive integers, N and K ( 1 ≤ K ≤ N ), the task is to construct an array arr[](1-based indexing) such that each array element arr[i] is either i or -i and the array contains exactly K positive values such that sum of the array elements is positive. If more than one such sequence can be generated, print any of them. Otherwise, print "-1
7 min read
Only integer with positive value in positive negative value in array
Given an array of N integers. In the given, for each positive element 'x' there exist a negative value '-x', except one integer whose negative value is not present. That integer may occur multiple number of time. The task is find that integer. Examples: Input : arr[] = { 1, 8, -6, -1, 6, 8, 8 } Output : 8 All positive elements have an equal negativ
8 min read
Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements
Given an array arr[] consisting of N positive integers, the task is to find the last positive array element remaining after repeated subtractions of the smallest positive array element from all array elements. Examples: Input: arr[] = {3, 5, 4, 7}Output: 2Explanation: Subtract the smallest positive element from the array, i.e. 3, the new array is a
6 min read
Check whether product of integers from a to b is positive , negative or zero
Given two integers a and b, the task is to check whether the product of integers from the range v[a, b] i.e. a * (a + 1) * (a + 2) * ... * b is positive, negative or zero. Examples: Input: a = -10, b = -2 Output: NegativeInput: a = -10, b = 2 Output: Zero Naive approach: We can run a loop from a to b and multiply all the numbers starting from a to
7 min read
Count sequences of positive integers having product X
Given an array arr[] of size N, the task is to find the total number of sequences of positive integers possible (greater than 1) whose product is exactly X. The value of X is calculated as the product of the terms where ith term is generated by raising ith prime number to the power of arr[i]. X = 2 ^ arr[1] * 3 ^ arr[2] * 5 ^ arr[3] * 7 ^ arr[4] *
9 min read
Find the missing digit in given product of large positive integers
Given two large integers in form of strings A and B and their product also in form of string C such that one digit of the product is replaced with X, the task is to find the replaced digit in the product C. Examples: Input: A = 51840, B = 273581, C = 1418243x040Output: 9Explanation:The product of integer A and B is 51840 * 273581 = 14182439040. On
11 min read
Minimum operations for which all integers from [0, N] appears as smallest positive missing number (MEX)
Given an array arr[], of size N the task is to find the minimum operations on the array such that in each operation any element in the array can be chosen and incremented by 1 so that the MEX is i for all i in the range [0, n]. If for any i if the MEX is not i print -1. Examples : Input : arr[] = {3, 0, 1, 0, 3}Output: MEX for i = 0 is 2MEX for i =
13 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg