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
function sum ( arr , N , K ) { var sum = 0 ; var v = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { v . push ( arr [ i ] ) ; } for ( var i = 0 ; i < v . length ; i ++ ) { if ( v [ i ] % K == 0 ) { var x = v [ i ] / K ; for ( var j = 0 ; j < K ; j ++ ) { v . push ( x ) ; } } else break ; } for ( var i = 0 ; i < v . length ; i ++ ) sum = sum + v [ i ] ; return sum ; } var arr = [ 4 , 6 , 8 , 2 ] ; var K = 2 ; var N = arr . length ; document . write ( sum ( arr , N , K ) ) ;