Driver code
let N = 8 , queries = 6 ; let Q = [ [ 1 , 4 ] , [ 3 , 4 ] , [ 4 , 5 ] , [ 5 , 5 ] , [ 7 , 8 ] , [ 8 , 8 ] ] ; let A = Array . from ( { length : N } , ( _ , i ) => 0 ) ; document . write ( subarrayLength ( A , Q , N , queries ) ) ;