# Minimum product of k integers in an array of positive integers

Let us understand the question: We need to find out the product of k elements in an array of size n, where k<=n.

Let us take an example:

If the array is: [10,5,4,7,8,1,2] and the k value is 2,

We need to find the minimum possible product by multiplying any two numbers in the vector.

And we know that we will be getting the minimum product only when we multiply the minimum values of the vector.

So first, we need to find out the k minimum elements of the vector and multiply them.

Instead of finding them, we can sort the array and multiply the first k numbers.

When we sort the above array, it becomes: [1,2,4,5,7,8,10]

The k value is 2, so we multiply the first 2 numbers; they are 1,2

Product=1*2=2.

If the k value is 5, the minimum possible product is 1*2*4*5*7=280.

So now we need to implement a code that takes array size and array elements as input, then asks the user to enter the k value, and then we need to sort the given array. Finally, we will multiply the first k values for the desired result.

So, let us implement the code:

CODE:

Output:

Explanation:

First, in the code, we need to take the input for the array size and the array elements.

This part of the code will take the size of the input array and store the array elements in an array named vec.

Next, we need to sort the given input array, and it is done by using the:

Next, we prompt the user to enter the size k, and then we need to print the minimum product possible with any of the k elements from the array.

So, to get a minimum product, we multiply the first k values, so we need to perform the following:

Val is initialized to 1, and we will multiply the val with the first k elements of the array.

We will get the minimum product possible with k elements of the array.

Here, we need to handle a case where k>n; when this case meets, it is an indication of a wrong request, so when the user gives k value such that k>n, then we will print "Cannot multiply k cannot be greater than n".

It is handled by:

We print the error message and exit using the above if condition, k>n.

Finally, we will print the value that is obtained after multiplying k elements, and we will also print the elements that give the product value.

i.e., we also print the first k elements of the array.

Using the above code, the vector's val and first k elements will be printed.

Let us also see when we give k>n:

In the above output, we can see that n is 5, and the k value is 8; here, k>n, so we cannot operate, so we will print as "Cannot multiply k cannot be greater than n".