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 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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll maxProd( int arr[], int n, int k)
{
ll product = 1;
unordered_map< int , int > s;
sort(arr, arr + n);
for ( int i = 0; i < n; i++) {
if (s[arr[i]] == 0) {
product = product * arr[i];
}
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)) {
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;
}
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
def maxProd(arr, n, k) :
product = 1 ;
s = dict .fromkeys(arr, 0 );
arr.sort();
for i in range (n) :
if (s[arr[i]] = = 0 ) :
product = product * arr[i];
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 )) :
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;
if __name__ = = "__main__" :
arr = [ 5 , 6 , 7 , 8 , 2 , 5 , 6 , 8 ];
n = len (arr) ;
k = 2 ;
print (maxProd(arr, n, k));
|
Java
import java.util.*;
class GFG
{
static long maxProd( int arr[], int n, int k)
{
long product = 1 ;
HashMap<Integer,Integer> s = new HashMap<Integer,Integer>();
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++)
{
if (s.containsKey(arr[i]) == false )
{
product = product * arr[i];
s.put(arr[i], 1 );
}
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 ))
{
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;
}
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));
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static long maxProd( int []arr, int n, int k)
{
long product = 1;
Dictionary< int , int > s = new Dictionary< int , int >();
Array.Sort(arr);
for ( int i = 0; i < n; i++)
{
if (!s.ContainsKey(arr[i]))
{
product = product * arr[i];
s[arr[i]] = 1;
}
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))
{
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;
}
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));
}
}
|
Javascript
function maxProd(arr, n, k) {
let product = 1;
let s = {};
arr.sort((a, b) => a - b);
for (let i = 0; i < n; i++) {
if (!s[arr[i]]) {
product *= arr[i];
}
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) {
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;
}
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
Share your thoughts in the comments
Please Login to comment...