Sum of elements in given range from Array formed by infinitely concatenating given array
Last Updated :
03 Aug, 2021
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>
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>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Sum of the first M elements of Array formed by infinitely concatenating given array
Given an array arr[] consisting of N integers and a positive integer M, the task is to find the sum of the first M elements of the array formed by the infinite concatenation of the given array arr[]. Examples: Input: arr[] = {1, 2, 3}, M = 5Output: 9Explanation:The array formed by the infinite conca
5 min read
Check if given Array can be formed from initial Array having one element
Given array A[], the task for this problem is to check whether A[] can be formed from array B[] = {1} by performing the following operation any number of times, select a subsequence of B[] and insert total sum of this subsequence in B[] to any position. Print "YES" if it is possible "NO otherwise. E
7 min read
Maximize array sum by concatenating corresponding elements of given two arrays
Given two array A[] and B[] of the same length, the task is to find the maximum array sum that can be formed by joining the corresponding elements of the array in any order. Input: A[] = {1, 2, 3, 4, 5}, B[] = {3, 2, 1, 4, 5} Output: 183 Explanation: Numbers formed by joining the digits of the eleme
8 min read
Count of Ways to obtain given Sum from the given Array elements
Given an array arr[], consisting of N non-negative integers and an integer S, the task is to find the number of ways to obtain the sum S by adding or subtracting array elements. Note: All the array elements need to be involved in generating the sum. Examples: Input: arr[] = {1, 1, 1, 1, 1}, S = 3 Ou
15+ min read
Count of non decreasing Arrays with ith element in range [A[i], B[i]]
Given two arrays A[] and B[] both consisting of N integers, the task is to find the number of non-decreasing arrays of size N that can be formed such that each array element lies over the range [A[i], B[i]]. Examples: Input: A[] = {1, 1}, B[] = {2, 3}Output: 5Explanation:The total number of valid ar
9 min read
Find original Array from given Array where each element is sum of prefix and postfix sum
Given an array arr[] of length N, where arr is derived from an array nums[] which is lost. Array arr[] is derived as: arr[i] = (nums[0] + nums[1] + ... + nums[i]) + (nums[i] + nums[i+1] + ... + nums[N-1]). The task is to find nums[] array of length N. Examples: Input: N = 4, arr[] = {9, 10, 11, 10}O
10 min read
Construct sum-array with sum of elements in given range
You are given an array of n-elements and an odd-integer m. You have to construct a new sum_array from given array such that sum_array[i] = ?arr[j] for (i-(m/2)) < j (i+(m/2)). note : for 0 > j or j >= n take arr[j] = 0. Examples: Input : arr[] = {1, 2, 3, 4, 5}, m = 3 Output : sum_array = {
7 min read
Check if sum can be formed by selecting an element for each index from two Arrays
Given two arrays arr1[] and arr2[] of size N each. Given target sum M, Choose either a[i] or b[i] for each i where (0 ? i < N), the task is to check if it is possible to achieve the target sum print "Yes" otherwise "No". Examples: Input: arr1[] = {3, 4}, arr2[] = {6, 5}, M = 10Output: YesExplanat
15+ min read
Maximize Array sum by adding multiple of another Array element in given ranges
Given two arrays X[] and Y[] of length N along with Q queries each of type [L, R] that denotes the subarray of X[] from L to R. The task is to find the maximum sum that can be obtained by applying the following operation for each query: Choose an element from Y[]. Add multiples with alternate +ve an
15+ min read
Generate an array having sum of Bitwise OR of same-indexed elements with given array equal to K
Given an array arr[] consisting of N integers and an integer K, the task is to print an array generated such that the sum of Bitwise OR of same indexed elements of the generated array with the given array is equal to K. If it is not possible to generate such an array, then print "-1". Examples: Inpu
7 min read