Driver Code
public static void main ( String [ ] args ) { int a [ ] = { 1 , 1 , 2 , 1 , 3 } ; int n = a . length ; Query [ ] queries = new Query [ 3 ] ; for ( int i = 0 ; i < 3 ; i ++ ) queries [ i ] = new Query ( ) ; queries [ 0 ] . l = 0 ; queries [ 0 ] . r = 4 ; queries [ 0 ] . idx = 0 ; queries [ 1 ] . l = 1 ; queries [ 1 ] . r = 3 ; queries [ 1 ] . idx = 1 ; queries [ 2 ] . l = 2 ; queries [ 2 ] . r = 4 ; queries [ 2 ] . idx = 2 ; int q = queries . length ; Arrays . sort ( queries , new Comparator < Query > ( ) { public int compare ( Query x , Query y ) { if ( x . r < y . r ) return - 1 ; else if ( x . r == y . r ) return 0 ; else return 1 ; } } ) ; answeringQueries ( a , n , queries , q ) ; } }