Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | Function to calculate sum of array elements after adding arr [ i ] / K to the end of the array if arr [ i ] is divisible by K ; Stores the sum of the array ; Traverse the array arr [ ] ; Traverse the vector ; If v [ i ] is divisible by K ; Iterate over the range [ 0 , K ] ; Update v ; Otherwise ; Traverse the vector v ; Return the sum of the updated array ; Driver Code
def summ ( arr , N , K ) : NEW_LINE INDENT sum = 4 NEW_LINE v = [ i for i in arr ] NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if ( v [ i ] % K == 0 ) : NEW_LINE INDENT x = v [ i ] // K NEW_LINE for j in range ( K ) : NEW_LINE INDENT v . append ( x ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT sum = sum + v [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 6 , 8 , 2 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( summ ( arr , N , K ) ) NEW_LINE DEDENT