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 ; Print the result ; Driver Code
def updateArray ( arr , K ) : NEW_LINE INDENT sum = 0 NEW_LINE res = [ ] NEW_LINE for i in range ( 0 , int ( len ( arr ) ) ) : NEW_LINE INDENT if ( arr [ i ] < K ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT if ( sum != 0 ) : NEW_LINE INDENT res . append ( sum ) NEW_LINE DEDENT sum = 0 NEW_LINE res . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT if ( sum != 0 ) : NEW_LINE INDENT res . append ( sum ) NEW_LINE DEDENT for it in res : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 200 , 6 , 36 , 612 , 121 , 66 , 63 , 39 , 668 , 108 ] NEW_LINE K = 100 NEW_LINE updateArray ( arr , K ) NEW_LINE DEDENT