Open In App

Sum of elements in given range from Array formed by infinitely concatenating given array

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[](1-based indexing) consisting of N positive integers and two positive integers L and R,  the task is to find the sum of array elements over the range [L, R] if the given array arr[] is concatenating to itself infinite times.

Examples:

Input: arr[] = {1, 2, 3}, L = 2, R = 8
Output: 14
Explanation:
The array, arr[] after concatenation is {1, 2, 3, 1, 2, 3, 1, 2, …} and the sum of elements from index 2 to 8 is 2 + 3 + 1 + 2 + 3 + 1 + 2 = 14.

Input: arr[] = {5, 2, 6, 9}, L = 10, R = 13
Output: 22

Naive Approach: The simplest approach to solve the given problem is to iterate over the range [L, R] using the variable i and add the value of arr[i % N] to the sum for each index. After completing the iteration, print the value of the sum as the resultant sum.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the sum of elements
// in a given range of an infinite array
void rangeSum(int arr[], int N, int L, int R)
{
    // Stores the sum of array elements
    // from L to R
    int sum = 0;
 
    // Traverse from L to R
    for (int i = L - 1; i < R; i++) {
        sum += arr[i % N];
    }
 
    // Print the resultant sum
    cout << sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = sizeof(arr) / sizeof(arr[0]);
    rangeSum(arr, N, L, R);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find the sum of elements
    // in a given range of an infinite array
    static void rangeSum(int arr[], int N, int L, int R)
    {
       
        // Stores the sum of array elements
        // from L to R
        int sum = 0;
 
        // Traverse from L to R
        for (int i = L - 1; i < R; i++) {
            sum += arr[i % N];
        }
 
        // Print the resultant sum
        System.out.println(sum);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 5, 2, 6, 9 };
        int L = 10, R = 13;
        int N = arr.length;
        rangeSum(arr, N, L, R);
    }
}
 
// This code is contributed by Potta Lokesh


Python3




# Python 3 program for the above approach
 
# Function to find the sum of elements
# in a given range of an infinite array
def rangeSum(arr, N, L, R):
   
    # Stores the sum of array elements
    # from L to R
    sum = 0
 
    # Traverse from L to R
    for i in range(L - 1,R,1):
        sum += arr[i % N]
 
    # Print the resultant sum
    print(sum)
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 2, 6, 9 ]
    L = 10
    R = 13
    N = len(arr)
    rangeSum(arr, N, L, R)
     
    # This code is contributed by divyeshrabadiya07


C#




// C# program for the above approach
using System;
class GFG {
 
    // Function to find the sum of elements
    // in a given range of an infinite array
    static void rangeSum(int[] arr, int N, int L, int R)
    {
 
        // Stores the sum of array elements
        // from L to R
        int sum = 0;
 
        // Traverse from L to R
        for (int i = L - 1; i < R; i++) {
            sum += arr[i % N];
        }
 
        // Print the resultant sum
        Console.Write(sum);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] arr = { 5, 2, 6, 9 };
        int L = 10, R = 13;
        int N = arr.Length;
        rangeSum(arr, N, L, R);
    }
}
 
// This code is contributed by ukasp.


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to find the sum of elements
// in a given range of an infinite array
function rangeSum(arr, N, L, R)
{
     
    // Stores the sum of array elements
    // from L to R
    let sum = 0;
 
    // Traverse from L to R
    for(let i = L - 1; i < R; i++)
    {
        sum += arr[i % N];
    }
 
    // Print the resultant sum
    document.write(sum);
}
 
// Driver Code
let arr = [ 5, 2, 6, 9 ];
let L = 10, R = 13;
let N = arr.length
 
rangeSum(arr, N, L, R);
 
// This code is contributed by _saurabh_jaiswal
 
</script>


Output: 

22

 

Time Complexity: O(R – L) 
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by using the Prefix Sum. Follow the steps below to solve the problem:

  • Initialize an array, say prefix[] of size (N + 1) with all elements as 0s.
  • Traverse the array, arr[] using the variable i and update prefix[i] to sum of prefix[i – 1] and arr[i – 1].
  • Now, the sum of elements over the range [L, R] is given by:

the sum of elements in the range [1, R] – sum of elements in the range [1, L – 1].

  • Initialize a variable, say leftSum as ((L – 1)/N)*prefix[N] + prefix[(L – 1)%N] to store the sum of elements in the range [1, L-1].
  • Similarly, initialize another variable rightSum as (R/N)*prefix[N] + prefix[R%N] to store the sum of elements in the range [1, R].
  • After completing the above steps, print the value of (rightSum – leftSum) as the resultant sum of elements over the given range [L, R].

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the sum of elements
// in a given range of an infinite array
void rangeSum(int arr[], int N, int L,
              int R)
{
    // Stores the prefix sum
    int prefix[N + 1];
    prefix[0] = 0;
 
    // Calculate the prefix sum
    for (int i = 1; i <= N; i++) {
        prefix[i] = prefix[i - 1]
                    + arr[i - 1];
    }
 
    // Stores the sum of elements
    // from 1 to L-1
    int leftsum
        = ((L - 1) / N) * prefix[N]
          + prefix[(L - 1) % N];
 
    // Stores the sum of elements
    // from 1 to R
    int rightsum = (R / N) * prefix[N]
                   + prefix[R % N];
 
    // Print the resultant sum
    cout << rightsum - leftsum;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = sizeof(arr) / sizeof(arr[0]);
    rangeSum(arr, N, L, R);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the sum of elements
// in a given range of an infinite array
static void rangeSum(int arr[], int N, int L, int R)
{
   
    // Stores the prefix sum
    int prefix[] = new int[N+1];
    prefix[0] = 0;
 
    // Calculate the prefix sum
    for (int i = 1; i <= N; i++) {
        prefix[i] = prefix[i - 1]
                    + arr[i - 1];
    }
 
    // Stores the sum of elements
    // from 1 to L-1
    int leftsum
        = ((L - 1) / N) * prefix[N]
          + prefix[(L - 1) % N];
 
    // Stores the sum of elements
    // from 1 to R
    int rightsum = (R / N) * prefix[N]
                   + prefix[R % N];
 
    // Print the resultant sum
    System.out.print( rightsum - leftsum);
}
 
// Driver Code
public static void main (String[] args)
{
    int arr[] = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = arr.length;
    rangeSum(arr, N, L, R);
 
}
}
 
// This code is contributed by shivanisinghss2110


Python3




# Python 3 program for the above approach
 
# Function to find the sum of elements
# in a given range of an infinite array
def rangeSum(arr, N, L, R):
   
    # Stores the prefix sum
    prefix = [0 for i in range(N + 1)]
    prefix[0] = 0
 
    # Calculate the prefix sum
    for i in range(1,N+1,1):
        prefix[i] = prefix[i - 1] + arr[i - 1]
 
    # Stores the sum of elements
    # from 1 to L-1
    leftsum = ((L - 1) // N) * prefix[N] + prefix[(L - 1) % N]
 
    # Stores the sum of elements
    # from 1 to R
    rightsum = (R // N) * prefix[N] + prefix[R % N]
 
    # Print the resultant sum
    print(rightsum - leftsum)
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 2, 6, 9]
    L = 10
    R = 13
    N = len(arr)
    rangeSum(arr, N, L, R)
 
    # This code is contributed by SURENDRA_GANGWAR.


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the sum of elements
// in a given range of an infinite array
static void rangeSum(int []arr, int N, int L, int R)
{
   
    // Stores the prefix sum
    int []prefix = new int[N+1];
    prefix[0] = 0;
 
    // Calculate the prefix sum
    for (int i = 1; i <= N; i++) {
        prefix[i] = prefix[i - 1]
                    + arr[i - 1];
    }
 
    // Stores the sum of elements
    // from 1 to L-1
    int leftsum
        = ((L - 1) / N) * prefix[N]
          + prefix[(L - 1) % N];
 
    // Stores the sum of elements
    // from 1 to R
    int rightsum = (R / N) * prefix[N]
                   + prefix[R % N];
 
    // Print the resultant sum
    Console.Write( rightsum - leftsum);
}
 
// Driver Code
public static void Main (String[] args)
{
    int []arr = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = arr.Length;
    rangeSum(arr, N, L, R);
 
}
}
 
// This code is contributed by shivanisinghss2110


Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to find the sum of elements
// in a given range of an infinite array
function rangeSum(arr, N, L, R) {
  // Stores the prefix sum
  let prefix = new Array(N + 1);
  prefix[0] = 0;
 
  // Calculate the prefix sum
  for (let i = 1; i <= N; i++) {
    prefix[i] = prefix[i - 1] + arr[i - 1];
  }
 
  // Stores the sum of elements
  // from 1 to L-1
  let leftsum = ((L - 1) / N) * prefix[N] + prefix[(L - 1) % N];
 
  // Stores the sum of elements
  // from 1 to R
  let rightsum = (R / N) * prefix[N] + prefix[R % N];
 
  // Print the resultant sum
  document.write(rightsum - leftsum);
}
 
// Driver Code
 
let arr = [5, 2, 6, 9];
let L = 10,
  R = 13;
let N = arr.length;
rangeSum(arr, N, L, R);
 
</script>


Output: 

22

 

Time Complexity: O(N)
Auxiliary Space: O(N)



Last Updated : 03 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads