# 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.

**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<bits/stdc++.h> ` `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<n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j=0; j<n; j++) ` ` ` `{ ` ` ` `// finding difference such that element ` ` ` `// having greater frequency is also ` ` ` `// greater in value. ` ` ` `if` `(freq[arr[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[0]); ` ` ` ` ` `cout << maxdiff(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find maximum difference ` `// between frequency of any two element ` `// such that element with greater frequency ` `// is also greater in value. ` `import` `java.util.*; ` `class` `GFG ` `{ ` ` ` `// Return the maximum difference between ` `// frequencies of any two elements such that ` `// element with greater frequency is also ` `// greater in value. ` `static` `int` `maxdiff(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `Map<Integer, Integer> freq = ` `new` `HashMap<>(); ` ` ` ` ` `// Finding the frequency of each element. ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `freq.put(arr[i], ` ` ` `freq.get(arr[i]) == ` `null` `? ` `1` `: ` ` ` `freq.get(arr[i]) + ` `1` `); ` ` ` ` ` `int` `ans = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < n; j++) ` ` ` `{ ` ` ` `// finding difference such that element ` ` ` `// having greater frequency is also ` ` ` `// greater in value. ` ` ` `if` `(freq.get(arr[i]) > freq.get(arr[j]) && ` ` ` `arr[i] > arr[j]) ` ` ` `ans = Math.max(ans, freq.get(arr[i]) - ` ` ` `freq.get(arr[j])); ` ` ` `else` `if` `(freq.get(arr[i]) < freq.get(arr[j]) && ` ` ` `arr[i] < arr[j] ) ` ` ` `ans = Math.max(ans, freq.get(arr[j]) - ` ` ` `freq.get(arr[i])); ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `3` `, ` `1` `, ` `3` `, ` `2` `, ` `3` `, ` `2` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(maxdiff(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python 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 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find maximum difference ` `// between frequency of any two element ` `// such that element with greater frequency ` `// is also greater in value. ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// Return the maximum difference between ` ` ` `// frequencies of any two elements such that ` ` ` `// element with greater frequency is also ` ` ` `// greater in value. ` ` ` `static` `int` `maxdiff(` `int` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` `Dictionary<` `int` `, ` ` ` `int` `> freq = ` `new` `Dictionary<` `int` `, ` ` ` `int` `>(); ` ` ` ` ` `// Finding the frequency of each element. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(freq.ContainsKey(arr[i])) ` ` ` `freq[arr[i]]++; ` ` ` `else` ` ` `freq.Add(arr[i], 1); ` ` ` `} ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < n; j++) ` ` ` `{ ` ` ` `// finding difference such that element ` ` ` `// having greater frequency is also ` ` ` `// greater in value. ` ` ` `if` `(freq[arr[i]] > freq[arr[j]] && arr[i] > arr[j]) ` ` ` `ans = Math.Max(ans, freq[arr[i]] - freq[arr[i]]); ` ` ` `else` `if` `(freq[arr[i]] < freq[arr[j]] && arr[i] < arr[j]) ` ` ` `ans = Math.Max(ans, freq[arr[j]] - freq[arr[i]]); ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `[] arr = { 3, 1, 3, 2, 3, 2 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(maxdiff(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(n^{2}).

**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 representation of this approach:

## C++

`// 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<bits/stdc++.h> ` `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<j; i++) ` ` ` `{ ` ` ` `int` `cur_freq = freq[dist[i]]; ` ` ` `ans = max(ans, cur_freq - min_freq); ` ` ` `min_freq = min(min_freq, cur_freq); ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 1, 3, 2, 3, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` ` ` `cout << maxdiff(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Efficient Python program to find maximum ` `# difference between frequency of any two ` `# elements such that element with greater ` `# frequency is also greater in value. ` ` ` `# 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 ` `=` `{} ` ` ` `dist ` `=` `[` `0` `] ` `*` `n ` ` ` ` ` `# Finding the frequency of each element. ` ` ` `j ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(arr[i] ` `not` `in` `freq): ` ` ` `dist[j] ` `=` `arr[i] ` ` ` `j ` `+` `=` `1` ` ` `freq[arr[i]] ` `=` `0` ` ` `if` `(arr[i] ` `in` `freq): ` ` ` `freq[arr[i]] ` `+` `=` `1` ` ` `dist ` `=` `dist[:j] ` ` ` ` ` `# Sorting the distinct element ` ` ` `dist.sort() ` ` ` `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 ` ` ` `ans ` `=` `0` ` ` `for` `i ` `in` `range` `(j): ` ` ` `cur_freq ` `=` `freq[dist[i]] ` ` ` `ans ` `=` `max` `(ans, cur_freq ` `-` `min_freq) ` ` ` `min_freq ` `=` `min` `(min_freq, cur_freq) ` ` ` ` ` `return` `ans ` ` ` `# Driven Program ` `arr ` `=` `[` `3` `, ` `1` `, ` `3` `, ` `2` `, ` `3` `, ` `2` `] ` `n ` `=` `len` `(arr) ` ` ` `print` `(maxdiff(arr, n)) ` ` ` `# This code is contributed by SHUBHAMSINGH10 ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity : **O(n log n).

This article is contributed by **Anuj Chauhan(anuj0503)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Next Greater Frequency Element
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Replace each element by the difference of the total size of the array and frequency of that element
- Maximum element in an array which is equal to its frequency
- Find the element having different frequency than other array elements
- Sum of all maximum frequency elements in Matrix
- Design a stack which can give maximum frequency element
- Longest subsequence with first and last element greater than all other elements
- Count of greater elements for each element in the Array
- Sum of elements in an array with frequencies greater than or equal to that element
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Sort elements by frequency | Set 1
- Sum of all odd frequency elements in a Matrix
- Sum of all even frequency elements in Matrix
- Sort elements by frequency | Set 2