Saturday, 23 Oct 2021

# Find maximum element among the elements with minimum frequency in given Array

Given an array arr[] consisting of N integers, the task is to find the maximum element with the minimum frequency.Examples:Input: arr[] = {2, 2, 5, 50, 1}Output: 50Explanation:The element with minimum frequency is {1, 5, 50}. The maximum element among these element is 50.Input: arr[] = {3, 2, 5, 6, 1}Output: 6Approach: The given problem can be solved by storing the frequency of the array element in a HashMap and then find the maximum value having a minimum frequency. Follow the step below to solve the given problem:Store the frequency of each element in a HashMap, say M.Initialize two variables, say maxValue as INT_MIN and minFreq as INT_MAX that store the resultant maximum element and stores the minimum frequency among all the frequencies.Iterate over the map M and perform the following steps:If the frequency of the current element is less than minFreq then update the value of minFreq to the current frequency and the value of maxValue to the current element.If the frequency of the current element is equal to the minFreq and the value of maxValue is less than the current value then then update the value of maxValue to the current element.After completing the above steps, print the value of maxValue as the resultant element.Below is the implementation of the above approach:C++  #include using namespace std;  int maxElementWithMinFreq(int* arr, int N){            unordered_map mp;              for (int i = 0; i < N; i++) {        mp[arr[i]]++;    }              int minFreq = INT_MAX;    int maxValue = INT_MIN;          for (auto x : mp) {          int num = x.first;        int freq = x.second;                                  if (freq < minFreq) {            minFreq = freq;            maxValue = num;        }                                          else if (freq == minFreq                 && maxValue < num) {            maxValue = num;        }    }          return maxValue;}  int main(){    int arr[] = { 2, 2, 5, 50, 1 };    int N = sizeof(arr) / sizeof(arr);    cout