Find sum in range L to R in given sequence of integers
Last Updated :
06 Jan, 2023
Given an array arr[] of integers and a sequence of the form:
[ 2, 3, 0, 1, 6, 7, 4, 5, … ].
Also given two integers L and R such that . The task is to find the sum of all numbers in a given range from L to R.
Examples:
Input : L = 0, R = 5
Output : 19
Explanation :
The arr[] is {2, 3, 0, 1, 6, 7}.
sum = arr[0] + arr[1] + arr[2] + arr[3] + arr[4] + arr[5]
sum = 2 + 3 + 0 + 1 + 6 + 7
Hence, the sum is 19.
Input : L = 2, R = 5
Output : 14
Explanation :
The arr[] is {0, 1, 6, 7}.
sum = arr[2] + arr[3] + arr[4] + arr[5]
sum = 0 + 1 + 6 + 7
Hence, the sum is 14.
Approach:
To solve the question mentioned above we first have to observe the sequence of the array and understand how is it generated. The given array is generated from a sequence of whole numbers which is [ 0, 1, 2, 3, 4, 5, 6, … ]. Initially, we add 2 to the first two integers, then we subtract 2 from the next two integers and this goes on. So our newly formed array looks like [ 0+2, 1+2, 2-2, 3-2, 4+2, 5+2, 6-2, 7-2, … ]. Hence we generate this new sequence of integers up to R and store it in array. Finally, calculate the sum from indices in range L to R and return it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int L, int R)
{
vector< int > arr;
int i = 0;
int x = 2;
while (i <= R) {
arr.push_back(i + x);
if (i + 1 <= R)
arr.push_back(i + 1 + x);
x *= -1;
i += 2;
}
int sum = 0;
for ( int i = L; i <= R; ++i)
sum += arr[i];
return sum;
}
int main()
{
int L = 0, R = 5;
cout << findSum(L, R);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int findSum( int L, int R)
{
ArrayList<Integer> arr = new ArrayList<>();
int i = 0 ;
int x = 2 ;
while (i <= R)
{
arr.add(i + x);
if (i + 1 <= R)
arr.add(i + 1 + x);
x *= - 1 ;
i += 2 ;
}
int sum = 0 ;
for (i = L; i <= R; ++i)
sum += arr.get(i);
return sum;
}
public static void main(String[] args)
{
int L = 0 , R = 5 ;
System.out.println(findSum(L, R));
}
}
|
Python3
def findSum(L, R) :
arr = []
i = 0
x = 2
k = 0
while (i < = R) :
arr.insert(k, i + x)
k + = 1
if (i + 1 < = R) :
arr.insert(k, i + 1 + x)
k + = 1
x * = - 1
i + = 2
sum = 0
for i in range (L, R + 1 ) :
sum + = arr[i]
return sum
L = 0
R = 5
print (findSum(L, R))
|
C#
using System;
using System.Collections;
class GFG{
public static int findSum( int L, int R)
{
ArrayList arr = new ArrayList();
int i = 0;
int x = 2;
while (i <= R)
{
arr.Add(i + x);
if (i + 1 <= R)
arr.Add(i + 1 + x);
x *= -1;
i += 2;
}
int sum = 0;
for (i = L; i <= R; ++i)
sum += ( int )arr[i];
return sum;
}
public static void Main( string [] args)
{
int L = 0, R = 5;
Console.Write(findSum(L, R));
}
}
|
Javascript
<script>
function findSum( L, R)
{
var arr=[];
var i = 0;
var x = 2;
while (i <= R) {
arr.push(i + x);
if (i + 1 <= R)
arr.push(i + 1 + x);
x *= -1;
i += 2;
}
var sum = 0;
for ( var i = L; i <= R; ++i)
sum += arr[i];
return sum;
}
var L = 0, R = 5;
document.write( findSum(L, R));
</script>
|
Time Complexity: O(R)
Auxiliary Space: O(R)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...