Maximum difference between frequency of two elements such that element having greater frequency is also greater | C # program to find maximum difference between frequency of any two element 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 . ; Finding the frequency of each element . ; finding difference such that element having greater frequency is also greater in value . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int maxdiff ( int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; 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 ++ ) { 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 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 3 , 2 , 3 , 2 } ; int n = arr . Length ; Console . WriteLine ( maxdiff ( arr , n ) ) ; } }