# Sum of elements in range L-R where first half and second half is filled with odd and even numbers

Given a number N, create an array such the first half of the array is filled with odd numbers till N and second half of the array is filled with even numbers. Also given are L and R indices, the task is to print the sum of elements in the array in the range [L, R].

Examples:

Input: N = 12, L = 1, R = 11
Output: 66
The array formed thus is {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12}
The sum between index 1 and index 11 is 66 {1+3+5+7+9+11+2+4+6+8+10}

Input: N = 11, L = 3 and R = 7
Output: 34
The array formed is {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10}
The sum between index 3 and index 7 is 34 {5+7+9+11+2}

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A naive approach will be to form the array of size N and iterate from L to R and return the sum.

## C++

 `// C++ program to find the sum between L-R ` `// range by creating the array ` `// Naive Approach ` `#include ` `using` `namespace` `std; ` ` `  `    ``// Function to find the sum between L and R ` `    ``int` `rangesum(``int` `n, ``int` `l, ``int` `r) ` `    ``{ ` `        ``// array created ` `        ``int` `arr[n]; ` ` `  `        ``// fill the first half of array ` `        ``int` `c = 1, i = 0; ` `        ``while` `(c <= n) { ` `            ``arr[i++] = c; ` `            ``c += 2; ` `        ``} ` ` `  `        ``// fill the second half of array ` `        ``c = 2; ` `        ``while` `(c <= n) { ` `            ``arr[i++] = c; ` `            ``c += 2; ` `        ``} ` `        ``int` `sum = 0; ` ` `  `        ``// find the sum between range ` `        ``for` `(i = l - 1; i < r; i++) { ` `            ``sum += arr[i]; ` `        ``} ` ` `  `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``int`  `main() ` `    ``{ ` `        ``int` `n = 12; ` `        ``int` `l = 1, r = 11; ` `        ``cout<<(rangesum(n, l, r)); ` `    ``} ` `// This code is contributed by ` `// Sanjit_Prasad `

## Java

 `// Java program to find the sum between L-R ` `// range by creating the array ` `// Naive Approach ` `import` `java.io.*; ` `public` `class` `GFG { ` ` `  `    ``// Function to find the sum between L and R ` `    ``static` `int` `rangesum(``int` `n, ``int` `l, ``int` `r) ` `    ``{ ` `        ``// array created ` `        ``int``[] arr = ``new` `int``[n]; ` ` `  `        ``// fill the first half of array ` `        ``int` `c = ``1``, i = ``0``; ` `        ``while` `(c <= n) { ` `            ``arr[i++] = c; ` `            ``c += ``2``; ` `        ``} ` ` `  `        ``// fill the second half of array ` `        ``c = ``2``; ` `        ``while` `(c <= n) { ` `            ``arr[i++] = c; ` `            ``c += ``2``; ` `        ``} ` `        ``int` `sum = ``0``; ` ` `  `        ``// find the sum between range ` `        ``for` `(i = l - ``1``; i < r; i++) { ` `            ``sum += arr[i]; ` `        ``} ` ` `  `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``12``; ` `        ``int` `l = ``1``, r = ``11``; ` `        ``System.out.println(rangesum(n, l, r)); ` `    ``} ` `} `

## Python3

 `# Python3 program to find the sum between L-R ` `# range by creating the array ` `# Naive Approach ` ` `  `# Function to find the sum between L and R ` `def` `rangesum(n, l, r): ` ` `  `    ``# array created ` `    ``arr ``=` `[``0``] ``*` `n; ` ` `  `    ``# fill the first half of array ` `    ``c ``=` `1``; i ``=` `0``; ` `    ``while` `(c <``=` `n): ` `        ``arr[i] ``=` `c; ` `        ``i ``+``=` `1``; ` `        ``c ``+``=` `2``; ` `     `  `    ``# fill the second half of array ` `    ``c ``=` `2``; ` `    ``while` `(c <``=` `n): ` `        ``arr[i] ``=` `c; ` `        ``i ``+``=` `1``; ` `        ``c ``+``=` `2``; ` ` `  `    ``sum` `=` `0``; ` ` `  `    ``# find the sum between range ` `    ``for` `i ``in` `range``(l ``-` `1``, r, ``1``): ` `        ``sum` `+``=` `arr[i]; ` `     `  `    ``return` `sum``; ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``n ``=` `12``; ` `    ``l, r ``=` `1``, ``11``; ` `    ``print``(rangesum(n, l, r)); ` `     `  `# This code contributed by PrinciRaj1992 `

## C#

 `// C# program to find the sum  ` `// between L-R range by creating  ` `// the array Naive Approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to find the ` `    ``// sum between L and R ` `    ``static` `int` `rangesum(``int` `n, ` `                        ``int` `l, ``int` `r) ` `    ``{ ` `        ``// array created ` `        ``int``[] arr = ``new` `int``[n]; ` ` `  `        ``// fill the first  ` `        ``// half of array ` `        ``int` `c = 1, i = 0; ` `        ``while` `(c <= n)  ` `        ``{ ` `            ``arr[i++] = c; ` `            ``c += 2; ` `        ``} ` ` `  `        ``// fill the second  ` `        ``// half of array ` `        ``c = 2; ` `        ``while` `(c <= n) ` `        ``{ ` `            ``arr[i++] = c; ` `            ``c += 2; ` `        ``} ` `        ``int` `sum = 0; ` ` `  `        ``// find the sum  ` `        ``// between range ` `        ``for` `(i = l - 1; i < r; i++)  ` `        ``{ ` `            ``sum += arr[i]; ` `        ``} ` ` `  `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 12; ` `        ``int` `l = 1, r = 11; ` `        ``Console.WriteLine(rangesum(n, l, r)); ` `    ``} ` `} ` ` `  `// This code is contributed  ` `// by inder_verma. `

## PHP

 ` `

Output:

```66
```

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

Efficient Approach: Without building the array the problem can be solved in O(1) complexity. Consider the middle point of the sequence thus formed. Then there can be 3 possibilities for L and R.

• l and r both are on the right of middle point.
• l and r both are on the left of middle point.
• l is on the left of the middle point and r is on the left of the middle point.

For 1st and 2nd case, just find the number which corresponds to the l-th position from the start or middle and the number which corresponds to the r-th position from start or middle. The below formula can be used to find out the sum:

`sum = (no. of terms in the range)*(first term + last term)/2`

For the third case, consider it as [L-mid] and [mid-R] and then apply case 1 and case 2 formula as mentioned above.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the sum between L-R ` `// range by creating the array ` `// Naive Approach ` `#include ` `using` `namespace` `std; ` ` `  `// // Function to calculate the sum if n is even ` `int` `sumeven(``int` `n, ``int` `l, ``int` `r) ` `{ ` `    ``int` `sum = 0; ` `    ``int` `mid = n / 2; ` ` `  `    ``// both l and r are to the left of mid ` `    ``if` `(r <= mid) ` `    ``{ ` `        ``// first and last element ` `        ``int` `first = (2 * l - 1); ` `        ``int` `last = (2 * r - 1); ` ` `  `        ``// Total number of terms in ` `        ``// the sequence is r-l+1 ` `        ``int` `no_of_terms = r - l + 1; ` ` `  `        ``// use of formula derived ` `        ``sum = ((no_of_terms) * ((first + last))) / 2; ` `    ``} ` `     `  `    ``// both l and r are to the right of mid ` `    ``else` `if` `(l >= mid) ` `    ``{ ` `        ``// first and last element ` `        ``int` `first = (2 * (l - n / 2)); ` `        ``int` `last = (2 * (r - n / 2)); ` ` `  `        ``int` `no_of_terms = r - l + 1; ` ` `  `        ``// Use of formula derived ` `        ``sum = ((no_of_terms) * ((first + last))) / 2; ` `    ``} ` ` `  `    ``// left is to the left of mid and ` `    ``// right is to the right of mid ` `    ``else`  `    ``{ ` `         `  `        ``// Take two sums i.e left and  ` `        ``// right differently and add ` `        ``int` `sumleft = 0, sumright = 0; ` ` `  `        ``// first and last element ` `        ``int` `first_term1 = (2 * l - 1); ` `        ``int` `last_term1 = (2 * (n / 2) - 1); ` ` `  `        ``// total terms ` `        ``int` `no_of_terms1 = n / 2 - l + 1; ` ` `  `        ``// no of terms ` `        ``sumleft = ((no_of_terms1) *  ` `                  ``((first_term1 + last_term1))) / 2; ` ` `  `        ``// The first even number is 2 ` `        ``int` `first_term2 = 2; ` ` `  `        ``// The last element is given by 2*(r-n/2) ` `        ``int` `last_term2 = (2 * (r - n / 2)); ` `        ``int` `no_of_terms2 = r - mid; ` ` `  `        ``// formula applied ` `        ``sumright = ((no_of_terms2) *  ` `                   ``((first_term2 + last_term2))) / 2; ` `        ``sum = (sumleft + sumright); ` `    ``} ` `    ``return` `sum; ` `} ` ` `  `// Function to calculate the sum if n is odd ` `int` `sumodd(``int` `n, ``int` `l, ``int` `r) ` `{ ` ` `  `    ``// take ceil value if n is odd ` `    ``int` `mid = n / 2 + 1; ` `    ``int` `sum = 0; ` ` `  `    ``// // both l and r are to the left of mid ` `    ``if` `(r <= mid)  ` `    ``{ ` `        ``// first and last element ` `        ``int` `first = (2 * l - 1); ` `        ``int` `last = (2 * r - 1); ` ` `  `        ``// number of terms ` `        ``int` `no_of_terms = r - l + 1; ` ` `  `        ``// formula ` `        ``sum = ((no_of_terms) *  ` `             ``((first + last))) / 2; ` `    ``} ` ` `  `    ``// // both l and r are to the right of mid ` `    ``else` `if` `(l > mid)  ` `    ``{ ` ` `  `        ``// firts and last ter, ` `        ``int` `first = (2 * (l - mid)); ` `        ``int` `last = (2 * (r - mid)); ` ` `  `        ``// no of terms ` `        ``int` `no_of_terms = r - l + 1; ` ` `  `        ``// formula used ` `        ``sum = ((no_of_terms) *  ` `              ``((first + last))) / 2; ` `    ``} ` ` `  `    ``// If l is on left and r on right ` `    ``else`  `    ``{ ` ` `  `        ``// calculate separate sums ` `        ``int` `sumleft = 0, sumright = 0; ` ` `  `        ``// first half ` `        ``int` `first_term1 = (2 * l - 1); ` `        ``int` `last_term1 = (2 * mid - 1); ` ` `  `        ``// calculate terms ` `        ``int` `no_of_terms1 = mid - l + 1; ` `        ``sumleft = ((no_of_terms1) *  ` `                  ``((first_term1 + last_term1))) / 2; ` ` `  `        ``// second half ` `        ``int` `first_term2 = 2; ` `        ``int` `last_term2 = (2 * (r - mid)); ` `        ``int` `no_of_terms2 = r - mid; ` `        ``sumright = ((no_of_terms2) *  ` `                   ``((first_term2 + last_term2))) / 2; ` ` `  `        ``// add both halves ` `        ``sum = (sumleft + sumright); ` `    ``} ` `    ``return` `sum; ` `} ` ` `  `// Function to find the sum between L and R ` `int` `rangesum(``int` `n, ``int` `l, ``int` `r) ` `{ ` `    ``int` `sum = 0; ` ` `  `    ``// If n is even ` `    ``if` `(n % 2 == 0) ` `        ``return` `sumeven(n, l, r); ` ` `  `    ``// If n is odd ` `    ``else` `        ``return` `sumodd(n, l, r); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 12; ` `    ``int` `l = 1, r = 11; ` `    ``cout << (rangesum(n, l, r)); ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Java

 `// Java program to find the sum between L-R ` `// range by creating the array ` `// Efficient Approach ` `import` `java.io.*; ` `public` `class` `GFG { ` ` `  `    ``// // Function to calculate the sum if n is even ` `    ``static` `int` `sumeven(``int` `n, ``int` `l, ``int` `r) ` `    ``{ ` `        ``int` `sum = ``0``; ` `        ``int` `mid = n / ``2``; ` ` `  `        ``// both l and r are to the left of mid ` `        ``if` `(r <= mid) { ` `            ``// first and last element ` `            ``int` `first = (``2` `* l - ``1``); ` `            ``int` `last = (``2` `* r - ``1``); ` ` `  `            ``// Total number of terms in ` `            ``// the sequence is r-l+1 ` `            ``int` `no_of_terms = r - l + ``1``; ` ` `  `            ``// use of formula derived ` `            ``sum = ((no_of_terms) * ((first + last))) / ``2``; ` `        ``} ` `        ``// both l and r are to the right of mid ` `        ``else` `if` `(l >= mid) { ` `            ``// // first and last element ` `            ``int` `first = (``2` `* (l - n / ``2``)); ` `            ``int` `last = (``2` `* (r - n / ``2``)); ` ` `  `            ``int` `no_of_terms = r - l + ``1``; ` ` `  `            ``// Use of formula derived ` `            ``sum = ((no_of_terms) * ((first + last))) / ``2``; ` `        ``} ` ` `  `        ``// left is to the left of mid and ` `        ``// right is to the right of mid ` `        ``else` `{ ` `            ``// Take two sums i.e left and  ` `            ``// right differently and add ` `            ``int` `sumleft = ``0``, sumright = ``0``; ` ` `  `            ``// first and last element ` `            ``int` `first_term1 = (``2` `* l - ``1``); ` `            ``int` `last_term1 = (``2` `* (n / ``2``) - ``1``); ` ` `  `            ``// total terms ` `            ``int` `no_of_terms1 = n / ``2` `- l + ``1``; ` ` `  `            ``// no of terms ` `            ``sumleft = ((no_of_terms1) * ((first_term1 + last_term1))) / ``2``; ` ` `  `            ``// The first even number is 2 ` `            ``int` `first_term2 = ``2``; ` ` `  `            ``// The last element is given by 2*(r-n/2) ` `            ``int` `last_term2 = (``2` `* (r - n / ``2``)); ` `            ``int` `no_of_terms2 = r - mid; ` ` `  `            ``// formula applied ` `            ``sumright = ((no_of_terms2) * ((first_term2 + last_term2))) / ``2``; ` `            ``sum = (sumleft + sumright); ` `        ``} ` ` `  `        ``return` `sum; ` `    ``} ` ` `  `    ``// Function to calculate the sum if n is odd ` `    ``static` `int` `sumodd(``int` `n, ``int` `l, ``int` `r) ` `    ``{ ` ` `  `        ``// take ceil value if n is odd ` `        ``int` `mid = n / ``2` `+ ``1``; ` `        ``int` `sum = ``0``; ` ` `  `        ``// // both l and r are to the left of mid ` `        ``if` `(r <= mid) { ` `            ``// first and last element ` `            ``int` `first = (``2` `* l - ``1``); ` `            ``int` `last = (``2` `* r - ``1``); ` ` `  `            ``// number of terms ` `            ``int` `no_of_terms = r - l + ``1``; ` ` `  `            ``// formula ` `            ``sum = ((no_of_terms) * ((first + last))) / ``2``; ` `        ``} ` ` `  `        ``// // both l and r are to the right of mid ` `        ``else` `if` `(l > mid) { ` ` `  `            ``// firts and last ter, ` `            ``int` `first = (``2` `* (l - mid)); ` `            ``int` `last = (``2` `* (r - mid)); ` ` `  `            ``// no of terms ` `            ``int` `no_of_terms = r - l + ``1``; ` ` `  `            ``// formula used ` `            ``sum = ((no_of_terms) * ((first + last))) / ``2``; ` `        ``} ` ` `  `        ``// If l is on left and r on right ` `        ``else` `{ ` ` `  `            ``// calculate separate sums ` `            ``int` `sumleft = ``0``, sumright = ``0``; ` ` `  `            ``// first half ` `            ``int` `first_term1 = (``2` `* l - ``1``); ` `            ``int` `last_term1 = (``2` `* mid - ``1``); ` ` `  `            ``// calculate terms ` `            ``int` `no_of_terms1 = mid - l + ``1``; ` `            ``sumleft = ((no_of_terms1) * ((first_term1 + last_term1))) / ``2``; ` ` `  `            ``// second half ` `            ``int` `first_term2 = ``2``; ` `            ``int` `last_term2 = (``2` `* (r - mid)); ` `            ``int` `no_of_terms2 = r - mid; ` `            ``sumright = ((no_of_terms2) * ((first_term2 + last_term2))) / ``2``; ` ` `  `            ``// add both halves ` `            ``sum = (sumleft + sumright); ` `        ``} ` ` `  `        ``return` `sum; ` `    ``} ` `    ``// Function to find the sum between L and R ` `    ``static` `int` `rangesum(``int` `n, ``int` `l, ``int` `r) ` `    ``{ ` `        ``int` `sum = ``0``; ` ` `  `        ``// If n is even ` `        ``if` `(n % ``2` `== ``0``) ` `            ``return` `sumeven(n, l, r); ` ` `  `        ``// If n is odd ` `        ``else` `            ``return` `sumodd(n, l, r); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``12``; ` `        ``int` `l = ``1``, r = ``11``; ` `        ``System.out.println(rangesum(n, l, r)); ` `    ``} ` `} `

## C#

 `// C# program to find the sum  ` `// between L-R range by creating  ` `// the array Efficient Approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `    ``// Function to calculate ` `    ``// the sum if n is even ` `    ``static` `int` `sumeven(``int` `n,  ` `                       ``int` `l, ``int` `r) ` `    ``{ ` `        ``int` `sum = 0; ` `        ``int` `mid = n / 2; ` ` `  `        ``// both l and r are  ` `        ``// to the left of mid ` `        ``if` `(r <= mid)  ` `        ``{ ` `            ``// first and last element ` `            ``int` `first = (2 * l - 1); ` `            ``int` `last = (2 * r - 1); ` ` `  `            ``// Total number of terms in ` `            ``// the sequence is r-l+1 ` `            ``int` `no_of_terms = r - l + 1; ` ` `  `            ``// use of formula derived ` `            ``sum = ((no_of_terms) *  ` `                  ``((first + last))) / 2; ` `        ``} ` `         `  `        ``// both l and r are ` `        ``// to the right of mid ` `        ``else` `if` `(l >= mid) ` `        ``{ ` `            ``// first and last element ` `            ``int` `first = (2 * (l - n / 2)); ` `            ``int` `last = (2 * (r - n / 2)); ` ` `  `            ``int` `no_of_terms = r - l + 1; ` ` `  `            ``// Use of formula derived ` `            ``sum = ((no_of_terms) *  ` `                  ``((first + last))) / 2; ` `        ``} ` ` `  `        ``// left is to the left of  ` `        ``// mid and right is to the  ` `        ``// right of mid ` `        ``else`  `        ``{ ` `            ``// Take two sums i.e left and  ` `            ``// right differently and add ` `            ``int` `sumleft = 0, sumright = 0; ` ` `  `            ``// first and last element ` `            ``int` `first_term1 = (2 * l - 1); ` `            ``int` `last_term1 = (2 * (n / 2) - 1); ` ` `  `            ``// total terms ` `            ``int` `no_of_terms1 = n / 2 - l + 1; ` ` `  `            ``// no of terms ` `            ``sumleft = ((no_of_terms1) *  ` `                      ``((first_term1 +  ` `                        ``last_term1))) / 2; ` ` `  `            ``// The first even  ` `            ``// number is 2 ` `            ``int` `first_term2 = 2; ` ` `  `            ``// The last element is ` `            ``// given by 2*(r-n/2) ` `            ``int` `last_term2 = (2 * (r - n / 2)); ` `            ``int` `no_of_terms2 = r - mid; ` ` `  `            ``// formula applied ` `            ``sumright = ((no_of_terms2) *  ` `                       ``((first_term2 +  ` `                         ``last_term2))) / 2; ` `            ``sum = (sumleft + sumright); ` `        ``} ` ` `  `        ``return` `sum; ` `    ``} ` ` `  `    ``// Function to calculate  ` `    ``// the sum if n is odd ` `    ``static` `int` `sumodd(``int` `n,  ` `                      ``int` `l, ``int` `r) ` `    ``{ ` ` `  `        ``// take ceil value  ` `        ``// if n is odd ` `        ``int` `mid = n / 2 + 1; ` `        ``int` `sum = 0; ` ` `  `        ``// both l and r are ` `        ``// to the left of mid ` `        ``if` `(r <= mid)  ` `        ``{ ` `            ``// first and last element ` `            ``int` `first = (2 * l - 1); ` `            ``int` `last = (2 * r - 1); ` ` `  `            ``// number of terms ` `            ``int` `no_of_terms = r - l + 1; ` ` `  `            ``// formula ` `            ``sum = ((no_of_terms) *  ` `                  ``((first + last))) / 2; ` `        ``} ` ` `  `        ``// both l and r are ` `        ``// to the right of mid ` `        ``else` `if` `(l > mid)  ` `        ``{ ` ` `  `            ``// firts and last ter, ` `            ``int` `first = (2 * (l - mid)); ` `            ``int` `last = (2 * (r - mid)); ` ` `  `            ``// no of terms ` `            ``int` `no_of_terms = r - l + 1; ` ` `  `            ``// formula used ` `            ``sum = ((no_of_terms) * ` `                  ``((first + last))) / 2; ` `        ``} ` ` `  `        ``// If l is on left ` `        ``// and r on right ` `        ``else`  `        ``{ ` ` `  `            ``// calculate separate sums ` `            ``int` `sumleft = 0, sumright = 0; ` ` `  `            ``// first half ` `            ``int` `first_term1 = (2 * l - 1); ` `            ``int` `last_term1 = (2 * mid - 1); ` ` `  `            ``// calculate terms ` `            ``int` `no_of_terms1 = mid - l + 1; ` `            ``sumleft = ((no_of_terms1) *  ` `                      ``((first_term1 +  ` `                        ``last_term1))) / 2; ` ` `  `            ``// second half ` `            ``int` `first_term2 = 2; ` `            ``int` `last_term2 = (2 * (r - mid)); ` `            ``int` `no_of_terms2 = r - mid; ` `            ``sumright = ((no_of_terms2) *  ` `                       ``((first_term2 +  ` `                         ``last_term2))) / 2; ` ` `  `            ``// add both halves ` `            ``sum = (sumleft + sumright); ` `        ``} ` ` `  `        ``return` `sum; ` `    ``} ` `     `  `    ``// Function to find the ` `    ``// sum between L and R ` `    ``static` `int` `rangesum(``int` `n,  ` `                        ``int` `l, ``int` `r) ` `    ``{ ` `         `  `        ``// If n is even ` `        ``if` `(n % 2 == 0) ` `            ``return` `sumeven(n, l, r); ` ` `  `        ``// If n is odd ` `        ``else` `            ``return` `sumodd(n, l, r); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 12; ` `        ``int` `l = 1, r = 11; ` `        ``Console.WriteLine(rangesum(n, l, r)); ` `    ``} ` `} ` ` `  `// This code is contributed  ` `// by chandan_jnu. `

Output:

```66
```

Time complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up 