Array range queries for elements with frequency same as value | C # Program to answer Q queries to find number of times an element x appears x times in a Query subarray ; Returns the count of number x with frequency x in the subarray from start to end ; map for frequency of elements ; store frequency of each element in arr [ start ; end ] ; Count elements with same frequency as value ; Driver code ; 2D array of queries with 2 columns ; calculating number of queries
using System ; using System . Collections . Generic ; class GFG { public static int solveQuery ( int start , int end , int [ ] arr ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = start ; i <= end ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] ++ ; else mp . Add ( arr [ i ] , 1 ) ; } int count = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) { if ( entry . Key == entry . Value ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 2 , 3 , 3 , 3 } ; int n = A . Length ; int [ , ] queries = { { 0 , 1 } , { 1 , 1 } , { 0 , 2 } , { 1 , 3 } , { 3 , 5 } , { 0 , 5 } } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) { int start = queries [ i , 0 ] ; int end = queries [ i , 1 ] ; Console . WriteLine ( " Answer ▁ for ▁ Query ▁ " + ( i + 1 ) + " ▁ = ▁ " + solveQuery ( start , end , A ) ) ; } } }