
- 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 Equal Frequency in C++
Suppose we have an array nums of positive integers, we have to return the longest possible length of an array prefix of given array nums, such that it is possible to delete exactly one element from this prefix so that every number that has appeared in it will have the same frequency. After removing one element if there are no remaining elements, it's still considered that every appeared number has the same frequency (0).
So, if the input is like [3,3,2,2,6,4,4,6], then the output will be 7, So if we remove element 6 from index 4, then the subarray will be [3,3,2,2,4,4] where all elements appeared two times.
To solve this, we will follow these steps −
maxf := 0, res := 0
Define maps cnt and freq
for initialize i := 0, when i < size of nums, update (increase i by 1), do −
x := nums[i]
(increase cnt[x] by 1)
f := cnt[x]
(increase freq[f] by 1)
decrease freq[f - 1] by 1
maxf := maximum of maxf and f
if maxf * freq[maxf] is same as i or (maxf - 1) * (freq[maxf - 1] + 1) is same as i or maxf is same as 1, then −
res := i + 1
return res
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int maxEqualFreq(vector<int>& nums) { int maxf = 0, res = 0; map<int, int> cnt, freq; for (int i = 0; i < nums.size(); i++) { int x = nums[i]; cnt[x]++; int f = cnt[x]; freq[f]++; freq[f - 1]--; maxf = max(maxf, f); if (maxf * freq[maxf] == i || (maxf - 1) * (freq[maxf - 1] + 1) == i || maxf == 1) { res = i + 1; } } return res; } }; main(){ Solution ob; vector<int> v = {3,3,2,2,6,4,4,6}; cout << (ob.maxEqualFreq(v)); }
Input
{3,3,2,2,6,4,4,6}
Output
7
- Related Articles
- Maximum Frequency Stack in C++
- Maximum length subarray with LCM equal to product in C++
- Maximum subset with bitwise OR equal to k in C++
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k in C++
- Maximum Primes whose sum is equal to given N in C++
- Find maximum sum possible equal sum of three stacks in C++
- Maximum elements that can be made equal with k updates in C++
- Finding the maximum square sub-matrix with all equal elements in C++
- Maximum count of equal numbers in an array after performing given operations in C++
- Python – Extract elements with equal frequency as value
- Maximum sum subarray having sum less than or equal to given sums in C++
- Program to find maximum sum of two sets where sums are equal in C++
- Find maximum sum array of length less than or equal to m in C++
- Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
- Find maximum product of digits among numbers less than or equal to N in C++
