Given an **array arr[]** of integers and a sequence of the form:

[ 2, 3, 0, 1, 6, 7, 4, 5, … ].

Also given two integer **L and R** such that . The task is to find the sum of all numbers in given range from L to R.

**Examples:**

Input :L = 0, R = 5Output :19Explanation :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 = 5Output :14Explanation :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++

`// C++ program to find the ` `// sum in given range L to R ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the sum ` `// within the given range ` `int` `findSum(` `int` `L, ` `int` `R) ` `{ ` ` ` `vector<` `int` `> arr; ` ` ` ` ` `// generating array from given sequence ` ` ` `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; ` ` ` `} ` ` ` ` ` `// calculate the desired sum ` ` ` `int` `sum = 0; ` ` ` ` ` `for` `(` `int` `i = L; i <= R; ++i) ` ` ` ` ` `sum += arr[i]; ` ` ` ` ` `// return the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driven code ` `int` `main() ` `{ ` ` ` ` ` `// initialise the range ` ` ` `int` `L = 0, R = 5; ` ` ` ` ` `cout << findSum(L, R); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the ` `// sum in given range L to R ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the sum ` `// within the given range ` `public` `static` `int` `findSum(` `int` `L, ` `int` `R) ` `{ ` ` ` `ArrayList<Integer> arr = ` `new` `ArrayList<>(); ` ` ` ` ` `// Generating array from given sequence ` ` ` `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` `; ` ` ` `} ` ` ` ` ` `// Calculate the desired sum ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `for` `(i = L; i <= R; ++i) ` ` ` `sum += arr.get(i); ` ` ` ` ` `// return the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Initialise the range ` ` ` `int` `L = ` `0` `, R = ` `5` `; ` ` ` ` ` `System.out.println(findSum(L, R)); ` `} ` `} ` ` ` `// This code is contributed by jrishabh99 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the ` `# sum in given range L to R ` ` ` `# Function to find the sum ` `# within the given range ` `def` `findSum(L, R) : ` ` ` `arr ` `=` `[] ` ` ` ` ` `# generating array from given sequence ` ` ` `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` ` ` ` ` `# calculate the desired sum ` ` ` `sum` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(L, R ` `+` `1` `) : ` ` ` `sum` `+` `=` `arr[i] ` ` ` ` ` `# return the sum ` ` ` `return` `sum` ` ` `# Driver code ` ` ` `# initialise the range ` `L ` `=` `0` `R ` `=` `5` `print` `(findSum(L, R)) ` ` ` `# This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the ` `// sum in given range L to R ` `using` `System; ` `using` `System.Collections; ` ` ` `class` `GFG{ ` ` ` `// Function to find the sum ` `// within the given range ` `public` `static` `int` `findSum(` `int` `L, ` `int` `R) ` `{ ` ` ` `ArrayList arr = ` `new` `ArrayList(); ` ` ` ` ` `// Generating array from given sequence ` ` ` `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; ` ` ` `} ` ` ` ` ` `// Calculate the desired sum ` ` ` `int` `sum = 0; ` ` ` ` ` `for` `(i = L; i <= R; ++i) ` ` ` `sum += (` `int` `)arr[i]; ` ` ` ` ` `// return the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` ` ` `// Initialise the range ` ` ` `int` `L = 0, R = 5; ` ` ` ` ` `Console.Write(findSum(L, R)); ` `} ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

**Output:**

19

**Time Complexity :** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Generate minimum sum sequence of integers with even elements greater
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Find Prime Adam integers in the given range [L, R]
- Queries to find the count of integers in a range that contain the given pattern
- Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference
- Find original sequence from Array containing the sequence merged many times in order
- k-th missing element in increasing sequence which is not present in a given sequence
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Median in a stream of integers (running integers)
- Minimum product of k integers in an array of positive Integers
- Mode in a stream of integers (running integers)
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find pair with maximum GCD for integers in range 2 to N
- Longest sequence of positive integers in an array
- Find N integers with given difference between product and sum
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
- Program to find sum of the given sequence
- Find the subsequence with given sum in a superincreasing sequence

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.