Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 elements 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++




// 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;
}


Python




# 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


Java




// Java implementation of the approach
import java.util.*;
 
class GFG
{
    // Function to return the maximum product value
    static long maxProd(int arr[], int n, int k)
    {
     
        // To store the product
        long product = 1;
        HashMap<Integer,Integer> s = new HashMap<Integer,Integer>();
         
        // Sort the array
        Arrays.sort(arr);
     
        for (int i = 0; i < n; i++)
        {
            if (s.containsKey(arr[i]) == false)
            {
     
                // Efficiently finding product
                // including every element once
                product = product * arr[i];
                 
                s.put(arr[i], 1);
     
            }
     
            // Storing values in hash map
            else
                s.put(arr[i],s.get(arr[i]) +1);
     
        }
     
        for (int j = n - 1; j >= 0 && k > 0; j--)
        {
            if ((k > (s.get(arr[j]) - 1)) &&
                    ((s.get(arr[j]) - 1) > 0))
            {
     
                // Including the greater repeating values
                // so that product can be maximized
                product *= Math.pow(arr[j], s.get(arr[j]) - 1);
                k = k - s.get(arr[j]) + 1;
                s.put(arr[j], 0);
            }
            if (k <= (s.get(arr[j]) - 1) &&
                    ((s.get(arr[j]) - 1) > 0))
            {
                product *= Math.pow(arr[j], k);
                break;
            }
        }
     
        return product;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = { 5, 6, 7, 8, 2, 5, 6, 8 };
        int n = arr.length;
        int k = 2;
        System.out.println(maxProd(arr, n, k));
    }
}
 
// This code is contributed by ihritik


C#




// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
    // Function to return the maximum product value
    static long maxProd(int []arr, int n, int k)
    {
     
        // To store the product
        long product = 1;
        Dictionary<int, int> s = new Dictionary<int, int>();
        // Sort the array
        Array.Sort(arr);
     
        for (int i = 0; i < n; i++)
        {
            if (!s.ContainsKey(arr[i]))
            {
     
                // Efficiently finding product
                // including every element once
                product = product * arr[i];
     
                s[arr[i]] = 1;
            }
     
            // Storing values in hash map
                else
            s[arr[i]]++;
     
        }
     
        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 *= (long)Math.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 *= (long)Math.Pow(arr[j], k);
                break;
            }
        }
     
        return product;
    }
     
    // Driver code
    public static void Main ()
    {
        int []arr = { 5, 6, 7, 8, 2, 5, 6, 8 };
        int n = arr.Length;
        int k = 2;
        Console.WriteLine(maxProd(arr, n, k));
    }
}
 
// This code is contributed by ihritik


Javascript




// JavaScript implementation of the approach
function maxProd(arr, n, k) {
// To store the product
let product = 1;
let s = {};
 
// Sort the array
arr.sort((a, b) => a - b);
 
for (let i = 0; i < n; i++) {
if (!s[arr[i]]) {
// Efficiently finding product
// including every element once
product *= arr[i];
}
// Storing values in hash map
s[arr[i]] = (s[arr[i]] || 0) + 1;
}
 
for (let 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 *= Math.pow(arr[j], s[arr[j]] - 1);
k -= s[arr[j]] - 1;
s[arr[j]] = 0;
}
if (k <= s[arr[j]] - 1 && s[arr[j]] - 1 > 0) {
product *= Math.pow(arr[j], k);
break;
}
}
 
return product;
}
 
// Driver code
const arr = [5, 6, 7, 8, 2, 5, 6, 8];
const n = arr.length;
const k = 2;
console.log(maxProd(arr, n, k));


Output:

161280

Time Complexity: O(n logn)
Auxiliary Space: O(n)



Last Updated : 22 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads