Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k

Given an array arr[] and an integer k, the task is to find the maximum product from the array such that the frequency sum of all repeating elements in the product is ≤ 2 * k where frequency sum is the sum of frequencies of all the elements in the product that appear more than once. For example, if we choose a product 1 * 1 * 2 * 3 * 4 * 5 * 6 * 6 * 7 * 8 * 8 then the frequency sum of repeating element in this product is 6 as the repeating elements in the product are 1, 6 and 8 (frequency of 1 + frequency of 6 + frequency of 8) = (2 + 2 + 2) = 6

Examples:

Input: arr[] = {5, 6, 7, 8, 2, 5, 6, 8}, k = 2
Output: 161280
The products can be:
5 * 5 * 7 * 8 * 8 * 2 * 6 = 134400
5 * 8 * 8 * 7 * 6 * 6 * 2 = 161280
2 * 7 * 6 * 5 * 8 * 6 * 5 = 100800
Out of which 161280 is the maximum

Input: arr[] = {1, 5, 1, 5, 4, 3, 8}, k = 2
Output: 2400

Approach: First take the product of all the elements from the array (include only a single occurrence of the elements as a single occurrence will not affect the frequency sum). Now in order to maximize the product, sort the array and start taking all the remaining occurrences of the elements starting from the greatest element until the frequency sum doesn’t exceed 2 * k. Print the calculated product in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
  
// Function to return the maximum product value
ll maxProd(int arr[], int n, int k)
{
  
    // To store the product
    ll product = 1;
    unordered_map<int, int> s;
  
    // Sort the array
    sort(arr, arr + n);
  
    for (int i = 0; i < n; i++) {
        if (s[arr[i]] == 0) {
  
            // Efficiently finding product
            // including every element once
            product = product * arr[i];
        }
  
        // Storing values in hash map
        s[arr[i]] = s[arr[i]] + 1;
    }
  
    for (int j = n - 1; j >= 0 && k > 0; j--) {
        if ((k > (s[arr[j]] - 1)) && ((s[arr[j]] - 1) > 0)) {
  
            // Including the greater repeating values
            // so that product can be maximized
            product *= pow(arr[j], s[arr[j]] - 1);
            k = k - s[arr[j]] + 1;
            s[arr[j]] = 0;
        }
        if (k <= (s[arr[j]] - 1) && ((s[arr[j]] - 1) > 0)) {
            product *= pow(arr[j], k);
            break;
        }
    }
  
    return product;
}
  
// Driver code
int main()
{
    int arr[] = { 5, 6, 7, 8, 2, 5, 6, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    cout << maxProd(arr, n, k);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the maximum 
# product value 
def maxProd(arr, n, k) :
  
    # To store the product 
    product = 1
    s = dict.fromkeys(arr, 0); 
  
    # Sort the array 
    arr.sort(); 
  
    for i in range(n) :
        if (s[arr[i]] == 0) :
  
            # Efficiently finding product 
            # including every element once 
            product = product * arr[i]; 
  
        # Storing values in hash map 
        s[arr[i]] = s[arr[i]] + 1
  
    j = n - 1;
    while (j >= 0 and k > 0) :
          
        if ((k > (s[arr[j]] - 1)) and 
           ((s[arr[j]] - 1) > 0)) :
  
            # Including the greater repeating values 
            # so that product can be maximized 
            product *= pow(arr[j], s[arr[j]] - 1); 
            k = k - s[arr[j]] + 1
            s[arr[j]] = 0
          
        if (k <= (s[arr[j]] - 1) and 
                ((s[arr[j]] - 1) > 0)) : 
            product *= pow(arr[j], k); 
            break;
        j -= 1
          
    return product; 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 5, 6, 7, 8, 2, 5, 6, 8 ]; 
    n = len(arr) ;
    k = 2
      
    print(maxProd(arr, n, k)); 
      
# This code is contributed by Ryuga

chevron_right


Output:

161280


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Ryuga