📌  相关文章
📜  来自数组的最大乘积,使得乘积中所有重复元素的频率总和小于或等于 2 * k

📅  最后修改于: 2021-10-27 08:41:37             🧑  作者: Mango

给定一个数组arr[]和一个整数k ,任务是从数组中找到最大乘积,使得乘积中所有重复元素的频率总和≤ 2 * k其中频率总和是所有重复元素的频率总和产品中多次出现的元素。例如,如果我们选择一个产品1 * 1 * 2 * 3 * 4 * 5 * 6 * 6 * 7 * 8 * 8那么这个产品中重复元素的频率和是 6 因为产品中的重复元素是 1 ,6和8(1的频率+6的频率+8的频率)=(2+2+2)=6

例子:

方法:首先取数组中所有元素的乘积(只包括一次出现的元素,因为一次出现不会影响频率和)。现在为了最大化乘积,对数组进行排序并开始从最大元素开始获取元素的所有剩余出现次数,直到频率总和不超过2 * k 。最后打印计算出的乘积。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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 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;
}


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 s = new HashMap(); 
          
        // 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


Python3
# 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


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 s = new Dictionary(); 
        // 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


输出:
161280

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程