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 maximumInput: 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; } |
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 |
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< 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 |
161280
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.