# Maximum difference between frequency of two elements such that element having greater frequency is also greater

Given an array of n positive integers with many repeating elements. The task is to find maximum difference between the frequency of any two different elements, such that the element with greater frequency is also greater in value than the second integer.

Examples:

```Input :  arr[] = { 3, 1, 3, 2, 3, 2 }.
Output : 2
Frequency of 3 = 3.
Frequency of 2 = 2.
Frequency of 1 = 1.
Here difference of frequency of element 3 and 1 is = 3 - 1 = 2.
Also 3 > 1.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 (Use Hashing):
The naive approach can be, find the frequency of each element and for each element find the element having lesser value and lesser frequency than the current element.

Below is the implementation of this approach:

## C++

 `// C++ program to find maximum difference ` `// between frequency of any two element ` `// such that element with greater frequency ` `// is also greater in value. ` `#include ` `using` `namespace` `std; ` ` `  `// Return the maximum difference between ` `// frequencies of any two elements such that ` `// element with greater frequency is also ` `// greater in value. ` `int` `maxdiff(``int` `arr[], ``int` `n) ` `{ ` `    ``unordered_map<``int``, ``int``> freq; ` ` `  `    ``// Finding the frequency of each element. ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``freq[arr[i]]++; ` ` `  `    ``int` `ans = 0; ` `    ``for` `(``int` `i=0; i freq[arr[j]] && ` `                ``arr[i] > arr[j] ) ` `                ``ans = max(ans, freq[arr[i]]-freq[arr[j]]); ` `            ``else` `if` `(freq[arr[i]] < freq[arr[j]] && ` `                      ``arr[i] < arr[j] ) ` `                ``ans = max(ans, freq[arr[j]]-freq[arr[i]]); ` `        ``} ` `    ``} ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driven Program ` `int` `main() ` `{ ` `    ``int` `arr[] = { 3, 1, 3, 2, 3, 2 }; ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr); ` ` `  `    ``cout << maxdiff(arr, n) << endl; ` `    ``return` `0; ` `} `

## Python3

 `# pyhton program to find maximum difference  ` `# between frequency of any two element  ` `# such that element with greater frequency  ` `# is also greater in value.  ` ` `  `from` `collections ``import` `defaultdict ` ` `  `# Return the maximum difference between  ` `# frequencies of any two elements such that  ` `# element with greater frequency is also  ` `# greater in value.  ` `def` `maxdiff(arr, n): ` `    ``freq ``=` `defaultdict(``lambda``: ``0``) ` ` `  `    ``# Finding the frequency of each element. ` `    ``for` `i ``in` `range``(n): ` `        ``freq[arr[i]] ``+``=` `1` `    ``ans ``=` `0` `    ``for` `i ``in` `range``(n): ` `        ``for` `j ``in` `range``(n): ` ` `  `            ``# finding difference such that element  ` `            ``# having greater frequency is also  ` `            ``# greater in value.  ` `            ``if` `freq[arr[i]] > freq[arr[j]] ``and` `arr[i] > arr[j]: ` `                ``ans ``=` `max``(ans, freq[arr[i]] ``-` `freq[arr[j]]) ` `            ``elif` `freq[arr[i]] < freq[arr[j]] ``and` `arr[i] < arr[j]: ` `                ``ans ``=` `max``(ans, freq[arr[j]] ``-` `freq[arr[i]]) ` `    ``return` `ans ` ` `  ` `  `arr ``=` `[``3``,``1``,``3``,``2``,``3``,``2``] ` `n ``=` `len``(arr) ` `print``(maxdiff(arr,n)) ` ` `  `# This code is contributed by Shrikant13 `

Output:

```2
```

Time Complexity: O(n2).

Method 2 (Use Hashing and Sorting):
The idea is to find all the distinct elements and store in an array, say dist[ ]. Sort the distinct element array dist[] in increasing order. Now for any distinct element at index i, for all index j such that i > j > 0, find the element between index 0 to i-1 having minimum frequency. We can find frequency of an element in same way as method 1, i.e., storing frequencies in a hash table.
So do this for all i and find the maximum difference. To find the minimum frequency for all i maintain a prefix minimum.

Below is C++ representation of this approach:

 `// Efficient C++ program to find maximum ` `// difference between frequency of any two ` `// elements such that element with greater ` `// frequency is also greater in value. ` `#include ` `using` `namespace` `std; ` ` `  `// Return the maximum difference between ` `// frequencies of any two elements such that ` `// element with greater frequency is also ` `// greater in value. ` `int` `maxdiff(``int` `arr[], ``int` `n) ` `{ ` `    ``unordered_map<``int``, ``int``> freq; ` ` `  `    ``int` `dist[n]; ` ` `  `    ``// Finding the frequency of each element. ` `    ``int` `j = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``if` `(freq.find(arr[i]) == freq.end()) ` `            ``dist[j++] = arr[i]; ` ` `  `        ``freq[arr[i]]++; ` `    ``} ` ` `  `    ``// Sorting the distinct element ` `    ``sort(dist, dist + j); ` ` `  `    ``int` `min_freq = n+1; ` ` `  `    ``// Iterate through all sorted distinct elements. ` `    ``// For each distinct element, maintaining the ` `    ``// element with minimum frequency than that ` `    ``// element and also finding the maximum ` `    ``// frequency difference ` `    ``int` `ans = 0; ` `    ``for` `(``int` `i=0; i

Output:

```2
```

Time Complexity : O(n log n).

## tags:

Hash Sorting Hash Sorting