Queries for count of array elements with values in given range 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
function setElement ( arr , n , index , x ) { arr [ index ] = x ; } function getCount ( arr , n , l , r ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= l && arr [ i ] <= r ) { count ++ ; } } return count ; } function SolveQuery ( arr , n , Q ) { let x ; for ( let i = 0 ; i < Q . length ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { x = getCount ( arr , n , Q [ i ] [ 1 ] [ 0 ] , Q [ i ] [ 1 ] [ 1 ] ) ; document . write ( x + " " ) ; } else { setElement ( arr , n , Q [ i ] [ 1 ] [ 0 ] , Q [ i ] [ 1 ] [ 1 ] ) ; } } } let arr = [ 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 ] ; let n = arr . length ; let Q = [ [ 1 , [ 3 , 5 ] ] , [ 1 , [ 2 , 4 ] ] , [ 1 , [ 1 , 2 ] ] , [ 2 , [ 1 , 7 ] ] , [ 1 , [ 1 , 2 ] ] ] ; SolveQuery ( arr , n , Q ) ;