Reduce given array by replacing subarrays with values less than K with their sum | Function to replace all the subarray having values < K with their sum ; Stores the sum of subarray having elements less than K ; Stores the updated array ; Traverse the array ; Update the sum ; Otherwise , update the vector ; Prlet the result ; Driver Code
function updateArray ( arr , K ) { let sum = 0 ; let res = [ ] ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] < K ) { sum += arr [ i ] ; } else { if ( sum != 0 ) { res . push ( sum ) ; } sum = 0 ; res . push ( arr [ i ] ) ; } } if ( sum != 0 ) res . push ( sum ) ; for ( let it of res ) document . write ( it + ' ' ) ; } let arr = [ 200 , 6 , 36 , 612 , 121 , 66 , 63 , 39 , 668 , 108 ] ; let K = 100 ; updateArray ( arr , K ) ;