Sum of all subarrays of size K | Function to find the sum of all subarrays of size K ; Loop to consider every subarray of size K ; Initialize sum = 0 ; Calculate sum of all elements of current subarray ; Print sum of each subarray ; Driver Code ; Function Call
function calcSum ( arr , n , k ) { for ( var i = 0 ; i <= n - k ; i ++ ) { var sum = 0 ; for ( var j = i ; j < k + i ; j ++ ) sum += arr [ j ] ; document . write ( sum + " " ) ; } } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var n = arr . length ; var k = 3 ; calcSum ( arr , n , k ) ;