
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k in C++
In this tutorial, we will be discussing a program to find maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k.
For this we will be provided with an array and an integer k. Our task is to find the maximum product from the array given that the frequency sum of all digits must be smaller than or equal to 2 * k.
Example
#include <bits/stdc++.h> using namespace std; #define ll long long int //returning maximum product value 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]; } //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)){ 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; }
Output
161280
- Related Articles
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Find maximum product of digits among numbers less than or equal to N in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program
- Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++
- Count all subsequences having product less than K in C++
- Subarray Product Less Than K in C++
- Find maximum sum array of length less than or equal to m in C++
- Find three integers less than or equal to N such that their LCM is maximum in C++
- Count pairs in a sorted array whose product is less than k in C++
- Minimum swaps required to bring all elements less than or equal to k together in C++
- Find three integers less than or equal to N such that their LCM is maximum - C++
- Maximum sum subarray having sum less than or equal to given sums in C++
- Print triplets with sum less than or equal to k in C Program
- Product of non-repeating (distinct) elements in an Array in C++
- Find the value of k such that the polynomial $x^{2}-(k+6)x+2(2k-1)$ has sum of its zeros equal to half of their product.

Advertisements