Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Boltu vs Balti

By Fake_Death · Limits 500ms, 512 MB

Boltu and Balti are two friends. Boltu is a great programmer whereas Balti is a newbie programmer. But Balti thinks that he is also a great programmer, but Boltu can't accept Balti as a great programmer. Now Boltu challenges Balti to solve a task to prove himself as a great programmer. Balti accepts that challenge.

The task is Boltu will give him any 2 numbers N and M , he (Balti) has to find the sum of all numbers in the range N and M (inclusive).
Balti knows that the equation of finding sum from 1 to n is n(n+1)/2 n(n+1)/2 .

Balti is getting stuck to find it from N to M. Now he wants your help. Help him to win this challenge by solving this problem.


Each line of input contains two integers N and M (0 ≤ N,M ≤ 109). The numbers are separated by exactly one space. The end of file will indicate the end of input.


You have to find the sum of numbers between N and M and print it as follows:

Sum of X to Y is: Z;

Here X is the smaller number, Y is the larger number and Z is the sum of all numbers between N and M (inclusive).


2 6
3 7
Sum of 2 to 6 is -> 20;
Sum of 3 to 7 is -> 25;

In the first sample test case the sum of numbers from 2 to 6 is 2+3+4+5+6=20 2+3+4+5+6 = 20 .

In the second sample test case the sum of numbers from 3 to 7 is 3+4+5+6+7=25 3+4+5+6+7 = 25 .



56% Solution Ratio

Rizon_sunnyEarliest, Jul '17

Swarna_1603033Fastest, 0.0s

suhasiniLightest, 0 B

imamanik05Shortest, 125B


Login to submit


The equation to finding the summation of 1 to n is given in the description: n*(n+1) / 2 Let's assu...

Toph uses cookies. By continuing you agree to our Cookie Policy.