Driver code
const a = [ 1 , 1 , 2 , 2 , 2 , 3 ] ; const n = a . length ; const queries = [ [ 0 , 4 ] , [ 1 , 2 ] , [ 2 , 5 ] , [ 2 , 3 ] , [ 2 , 4 ] ] ; const q = queries . length ; let k = 3 ; document . write ( getFinalSum ( a , n , queries , q , k ) ) ;