Sum of the first M elements of Array formed by infinitely concatenating given array | Java program for the above approach ; 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
import java . io . * ; import java . lang . * ; class GFG { public static int sumOfFirstM ( int A [ ] , int N , int M ) { int sum = 0 ; for ( int i = 0 ; i < M ; i ++ ) { sum = sum + A [ i % N ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int M = 5 ; int N = arr . length ; System . out . println ( sumOfFirstM ( arr , N , M ) ) ; } }