Queries for count of array elements with values in given range with updates | Java 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
import java . util . * ; import java . lang . * ; 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 ++ ; } } return count ; } static void SolveQuery ( int arr [ ] , int n , ArrayList < List < Integer > > Q ) { int x ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { if ( Q . get ( i ) . get ( 0 ) == 1 ) { x = getCount ( arr , n , Q . get ( i ) . get ( 1 ) , Q . get ( i ) . get ( 2 ) ) ; System . out . print ( x + " ▁ " ) ; } else { setElement ( arr , n , Q . get ( i ) . get ( 1 ) , Q . get ( i ) . get ( 2 ) ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 } ; int n = arr . length ; ArrayList < List < Integer > > Q = new ArrayList < > ( ) ; Q . add ( Arrays . asList ( 1 , 3 , 5 ) ) ; Q . add ( Arrays . asList ( 1 , 2 , 4 ) ) ; Q . add ( Arrays . asList ( 1 , 1 , 2 ) ) ; Q . add ( Arrays . asList ( 2 , 1 , 7 ) ) ; Q . add ( Arrays . asList ( 1 , 1 , 2 ) ) ; SolveQuery ( arr , n , Q ) ; } }