Maximum product of subsequence of size k in C++


In this problem, we are given an array arr[] of integers and a number k. Our task is to create a program to find the Maximum product of subsequence of size k in C++.

Problem Description − Here, we need to find the subsequence of size k, 1<= k <= n which has the maximum product of its elements.

Let’s take an example to understand the problem,

Input

arr[] = {1, 5, 6, -2, 0, 4} , k = 3

Output

120

Explanation

The subsequence of size 3 that has the maximum product is (5, 6, 4). The product is 120.

Solution approach

To solve this problem, we will first sort the array arr[] and then based on the elements of arr[] and the value of k. The method changes as in the following cases −

Case 1 (if k is even) − The product can have all maximum k values except 0. Here, we also need to consider the negative value pairs. As their magnitude can also give the result of maximum.

Case 2 (if k is odd) − This is a bit complex condition and values define how the result needs to be calculated. This case needs to be further classified based on the max element of the array.

Case 2.1 (if max no. is positive) − this means the array is a mixture of positive and negative numbers. In this case, we will find the max k elements and also search for max pairs from the negative side (if possible that might give the result).

Case 2.2 (if max no. is Zero) − This means the array contains all negative elements and zero. In this case, the max result will be 0, as multiplying an odd number of negative elements will result in a negative number, which means 0 is the maximum product.

Case 2.3 (if max no. is negative) − This means the array contains only negative numbers. In this case, the maximum result will be provided by multiplying the elements with minimum magnitude i.e. the maximum array will help.

In this way, we need to keep a check on the value of elements as well as k. For optimum result. For this, we will keep the max and min both sides in array to check if the result can be yielded by multiplying negative pairs to the result.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int findMaxSubArrayProduct(int arr[], int n, int k) {
   sort(arr, arr + n);
   int maxProd = 1;
   int i = 0, j = 0;
   int maxprod, minprod;
   if (arr[n - 1] == 0 && (k % 2 == 1))
      return 0;
   if (arr[n - 1] <= 0 && (k % 2 == 1)) {
      for (i = n - 1; i >= n - k; i--)
         maxProd *= arr[i];
         return maxProd;
   }
   i = 0;
   j = n - 1;
   if (k % 2 == 1) {
      maxProd *= arr[j];
      j--;
      k--;
   }
   k = k/2;
   int it = 0;
   while(it < k){
      int minprod = arr[i] * arr[i + 1];
      int maxprod = arr[j] * arr[j - 1];
      if (minprod > maxprod) {
         maxProd *= minprod;
         i += 2;
      } else {
         maxProd *= maxprod;
         j -= 2;
      }
      it++;
   }
   return maxProd;
}
int main() {
   int arr[] = { 1, 5, 6, -2, 0, 4 };
   int n = sizeof(arr) / sizeof(arr[0]);
   int k = 3;
   cout<<"The maximum product of subsequence of size "<<k<<" is "<<findMaxSubArrayProduct(arr, n, k);
   return 0;
}

Output

The maximum product of subsequence of size 3 is 120

Updated on: 15-Sep-2020

349 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements