Queries for count of array elements with values in given range with updates | C # code for queries for number of elements that lie in range [ l , r ] ( with updates ) ; Function to set arr [ index ] = x ; Function to get count of elements that lie in range [ l , r ] ; Traverse array ; If element lies in the range [ L , R ] ; Increase count ; Function to solve each query ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void setElement ( int [ ] arr , int n , int index , int x ) { arr [ index ] = x ; } static int getCount ( int [ ] arr , int n , int l , int r ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= l && arr [ i ] <= r ) count += 1 ; } return count ; } static void SolveQuery ( int [ ] arr , int n , List < List < int > > Q ) { int x ; for ( int i = 0 ; i < Q . Count ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { x = getCount ( arr , n , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] ) ; Console . Write ( x + " ▁ " ) ; } else { setElement ( arr , n , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] ) ; } } } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 } ; int n = arr . Length ; List < List < int > > myList = new List < List < int > > ( ) ; myList . Add ( new List < int > { 1 , 3 , 5 } ) ; myList . Add ( new List < int > { 1 , 2 , 4 } ) ; myList . Add ( new List < int > { 1 , 1 , 2 } ) ; myList . Add ( new List < int > { 2 , 1 , 7 } ) ; myList . Add ( new List < int > { 1 , 1 , 2 } ) ; SolveQuery ( arr , n , myList ) ; } }