Sum of Range in a Series of First Odd then Even Natural Numbers


The problem statement includes finding the sum of range in a series of first odd numbers then even natural numbers up to N.

The sequence consists of all the odd natural numbers from 1 to N and then all the even natural numbers from 2 to N, including N. The sequence will be of size N. We will be provided with a range in the problem for which we need to find out the sum of the sequence within that range, a and b i.e. [a,b]. Here a and b are included in the range.

For example, we are given N=7.

The sequence will first have all the odd natural numbers from 1 to N i.e. 7 and then all the even numbers from 1 to 7. The value of N is also included in the sequence. Thus, the sequence will be:

1, 3, 5, 7, 2, 4, 6.

We need to find the sum of the sequence in the given range, a and b where a and b are included.

In this problem, the inputs a and b represent the range in which we must calculate the sum of the sequence, and N represents the total number of items in the sequence. Our task will be to print the sum of the sequence in the range [a,b].

Example

Input

a=3, b=6, N=8

Output

18

Explanation − The given value of N is 8 in the input. The sequence will be 1, 3, 5, 7, 2, 4, 6, 8 as first it will contain all the odd natural numbers till 8 and then even natural numbers till 8. The sum of the sequence in the range [3,6] will be:

5+7+2+4=18, which is our required output.

Input

a=1 , b=4, N=4

Output

10

Explanation − The value of N given is 4. The sequence contains all the odd numbers until N and then all the even numbers until N. The sequence will be 1, 3, 2, 4.

The sum of the sequence within the range [1,4] is 1+3+2+4=10, which is the output.

Let’s understand the algorithm to find out the sum of the specified sequence in the given range.

Algorithm

Making the sequence, putting it in an array, and then getting the sum of the provided range from the array can be a simplistic solution to the problem. Both the space and runtime requirements for this method are O(N).

We can think of a better approach than this by following the pattern in the sequence. Since we know the value of N, we need to form a sequence where it consists of all the odd natural numbers till N then all the even natural numbers till N.

The range will be given as [a,b], where a and b are included. We can simply find the sum of the sequence from 1 to b, and subtract it from the sum of the sequence from 1 to a−1. This way we can get the sum of the sequence from a to b.

Since the sequence has N numbers in it where first it contains all the odd numbers and then even numbers until N, the number of odd numbers in the sequence will be N/2 if the N is even number and (N/2)+1 if the value of N is an odd number.

We can find the number of odd numbers in the sequence for N by using the ceil() function.

Syntax

double x;
ceil(x);

This function returns the smallest integer greater than or equal to the parameter passed in the function. For example, x=2.4 the function will return 3 as it is the smallest integer which is greater than or equal to 2.4.

Using the ceil() function we find the number of odd numbers in the sequence by ceil(N/2.0) and number of even numbers in the sequence by N−ceil(N/2.0).

The sum of first N odd numbers can be found out using the formula for sum of first n terms of an AP as it forms an A.P. with first term as 1 and common difference as 2.

The formula is given as,

$\mathrm{S_{n}=\frac{n}{2}(2*a+(n−1)d),a=first\:term\:of\:the\:AP\:and\:d=common\:difference}$

$\mathrm{sum\:of\:first\:N\:odd\:numbers=\frac{N}{2}(2*1+(N−1)*2)}$

$\mathrm{=N^{2}}$

Similarly, using the same formula we find the sum of the first N even number. Here the first term will be 2 and the common difference will be also 2.

$\mathrm{sum\:of\:first\:N\:even\:numbers=\frac{N}{2}(2*2+(N−1)*2)}$

$\mathrm{=\frac{2*N}{2}(2+N−1)=N(N+1)}$

  • To find the sum of the sequence till b in the range [a,b], we will first check the number of odd numbers in the sequence using the formula. If the number of odd numbers is greater than the value of b, we will simply use the formula to find the sum of odd numbers until b i.e. b^2.

  • And for the case when the value of b is greater than the number of odd numbers we will find the number of even numbers in the sequence till b, by subtracting the value of b with the number of odd numbers and then use the formula to find the sum of first N even numbers to find the sum of all the even numbers till b.

Now using the same procedure we will find the sum of the sequence till a−1 and subtract it from the sum of the sequence till b, which will give us the sum of the sequence in the given range i.e. [a,b] where a and b are also included.

We will use this algorithm in our approach in order to solve the problem within constant time.

Approach

The steps to follow to implement the above algorithm in our approach to find the sum in the given range:

  • We will make a function to find the sum of the sequence from the first position to the pth position.

  • We will check if the number of odd numbers in the sequence of N numbers is greater than the specified p position, then we will simply return the sum as p^2 using the formula to find the sum of the odd numbers because the number of odd numbers till p will be the first p odd numbers.

  • If the number of odd numbers is less than p, then we will calculate the number of even numbers in the sequence till p position and use it in the formula to find the sum of even numbers.

  • Using the function, we will calculate the sum of the sequence up to b and subtract the sum of the sequence up to a−1 from it to get the sum of the sequence in the given range i.e. [a,b].

Example

//C++ program to find the sum of the sequence within the given range [a,b]
#include <bits/stdc++.h>

using namespace std;

//function to find the sum of the sequence of N up to p position
//1,3,5,7,...N-1,2,4,6.....N
long long Sum(long long p, long long N)
{
    long long s=0; //to store the sum of the sequence till p
    
	// to find the number of odd numbers in the sequence till N
	long long odd_numbers = ceil(N / 2.0);

//if p is less than the number of odd numbers, find the sum of first p odd numbers
	if (p <= odd_numbers){
	s = p * p; //using the formula for sum of first p odd numbers
	return s;
    }

	//to store the number of even numbers in the sequence till p
	long long even_numbers = p - odd_numbers;

	//add the sum of odd numbers using the formula n^2 and even numbers using n(n+1)
	s = ((odd_numbers*odd_numbers)+ even_numbers*(even_numbers+1));
	
	return s; //return sum
}

int main()
{
	long long N,a,b;
	N=100;
	a=42;
	b=87;
	
	//to find the sum within the range [a,b] by subtracting the sum till b with sum till a-1
	long long ans = Sum(b,N) - Sum(a-1,N);
	
	//printing the output
	cout<<"The sum of the sequence within the range ["<<a<<","<<b<<"] : "<<ans<<endl;

	return 0;
}

Output

The sum of the sequence within the range [42,87] : 2225

Time complexity− O(1) , as constant time is taken to find the sum of the sequence within the given range.

Space complexity − O(1) , because we didn’t use any extra space in our approach.

Conclusion

The problem of finding the sum of the sequence in the given range where the sequence consists of first odd natural numbers till N and then even numbers till N in the sequence was discussed in the algorithm. Following the pattern in the sequence, we came up with an efficient algorithm in C++ in this article which runs on the constant time and takes constant space.

I hope you understand the problem and the approach to solve it after reading this article.

Updated on: 28-Aug-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements