Sum of the first M elements of Array formed by infinitely concatenating given array | Function to find the sum of first M numbers formed by the infinite concatenation of the array A [ ] ; Stores the resultant sum ; Iterate over the range [ 0 , M - 1 ] ; Add the value A [ i % N ] to sum ; Return the resultant sum ; Driver Code
function sumOfFirstM ( A , N , M ) { let sum = 0 ; for ( let i = 0 ; i < M ; i ++ ) { sum = sum + A [ i % N ] ; } return sum ; } let arr = [ 1 , 2 , 3 ] ; let M = 5 ; let N = arr . length ; document . write ( sumOfFirstM ( arr , N , M ) ) ;