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 ; Stores the array elements ; Traverse the array ; Stores if the operation should be formed or not ; Traverse the vector V ; If flag is false and if v [ i ] is divisible by K ; Otherwise , set flag as true ; Increment the sum by v [ i % N ] ; Return the resultant sum ; Driver Code
def Sum ( arr , N , K ) : NEW_LINE INDENT sum = 0 NEW_LINE v = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT flag = False NEW_LINE i = 0 NEW_LINE lenn = len ( v ) NEW_LINE while ( i < lenn ) : NEW_LINE INDENT if ( flag == False and ( v [ i ] % K == 0 ) ) : NEW_LINE INDENT v . append ( v [ i ] // K ) NEW_LINE DEDENT else : NEW_LINE INDENT flag = True NEW_LINE DEDENT sum += v [ i % N ] NEW_LINE i += 1 NEW_LINE lenn = len ( v ) NEW_LINE DEDENT return sum NEW_LINE DEDENT arr = [ 4 , 6 , 8 , 2 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( Sum ( arr , N , K ) ) NEW_LINE