Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | Returns sum between two kth smallest element of array ; Sort the given array ; Below code is equivalent to ; Driver code
function sumBetweenTwoKth ( arr , k1 , k2 ) { arr . sort ( function ( a , b ) { return a - b } ) ; var result = 0 ; for ( var i = k1 ; i < k2 - 1 ; i ++ ) result += arr [ i ] ; return result ; } var arr = [ 20 , 8 , 22 , 4 , 12 , 10 , 14 ] ; var k1 = 3 , k2 = 6 ; var n = arr . length ; document . write ( sumBetweenTwoKth ( arr , k1 , k2 ) ) ;