# Boltu vs Balti

Limits: 500ms, 256 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.

This is a companion discussion topic for the original entry at https://toph.co/p/boltu-vs-balti

printf(“Sum of %d to %d is:%d\n”, m, n, sum);…printing the output in this format i am getting wrong answer. But my output is right for all kind of cases i tested. what can i do for this problem …Need help.

Using long long int data type ur code will be ac

I don’t understand. Sum = what? And will m an n be in scanf function?

I used unsigned long long for all data but judged as wrong what should I do

Check you printed “Sum” as “sum”. @KAS.shim1998

I have a confusion. You says

N and M. Which will look like : “Sum of X to Y is: Z;“
But output is much different.

which one is correct(JAVA)?
String str = “Sum of “+N+” to “+M+” is: “+sum+”;”;
Or
String str = “Sum of “+N+” to “+M+” is-> “+sum+”;”;
I tried both but it keep showing wrong…
Or what will be the correct format by the way??

there are no problem but still rejected.why??

#include <stdio.h>
int main(){
long long int N,M,X,Y;
while(scanf("%lld %lld",&N,&M)==2){
if(0<=N && N<M && M<=1000000000){
X=(M*(M+1))/2;
N=N-1;
Y=(N*(N+1))/2;
printf(“Sum of %lld to %lld is -> %lld;\n”, N+1, M, X-Y);
}
}
return 0;
}

The value of N may be smaller than M, so you must exchange two values when N is smaller than M.
Here is my solution using Python3.

``````#!/usr/bin/env python
# -*- coding: utf-8 -*-

if __name__ == '__main__':
while True:
try:
n, m = map(int, input().strip().split())
n, m = min(n, m), max(n, m)
s = (n + m) * (m - n + 1) // 2
print('Sum of {0} to {1} is -> {2};'.format(n, m, s))
except EOFError:
break
``````
``````def input_num(a,b):
total = 0
for i in range(a,b+1):
total += i
sum_num = total
return f'Sum of {a} to {b} is -> {sum_num};'
``````

print(input_num(3,7))

what is the problem here?

a maybe smaller than b, you should think about input_num(7, 3) to print “Sum of 7 to 3 is -> 25;”

#include<stdio.h>
#include<stdlib.h>
int main()
{
long long n=0,m=0;
scanf("%lld%lld",&n,&m);
if(m<n)
{
long long swap = n;
n=m;
m=swap;
}
long long diff = abs(n-m)+1;
long long sum = (diff*(2*n+(diff-1)))/2;

``````printf("Sum of %lld to %lld is -> %lld;",n,m,sum);
return 0;
``````

}

a maybe smaller than b, you should think about input_num(7, 3) to print “Sum of 7 to 3 is -> 25;”
so ‘range(a, b+1)’ should be ‘range(min(a,b), max(a,b) + 1)’

1 Like

#include <bits/stdc++.h>

using namespace std;

int main() {
unsigned long long int n,m;
cin>>n>>m;
if(n>m) {
int t=m;
m=n;
n=t;
}
cout<<"Sum of “<<n<<” to “<<m<<” is -> "<<(m+n)*(m-n+1)/2<<’;’;
}
I have the same algo. But where is the problem? Showing wrong answer.

This is too easy problem indeed
use this theorem
small=small-1;
printf(“Sum of %lld to %lld is -> %lld;\n”,small+1,big,big*(big+1)/2-(small*(small+1))/2);

#include<bits/stdc++.h>
using namespace std;

int main() {
long long n , m;
long long sum=0;
cin>>n>>m;
if(n<m){
for(int i=n;i<=m;i++){
sum=sum+i;
}
}
else{
for(int i=m;i<=n;i++){
sum=sum+i;
}
}
cout<<“Sum of “<<n<<” to “<<m<<” is -> “<<sum<<”;”;
return 0;
}
this code show the out put at the same what the problem want but it is not accepted

Of course, it will not. It is a End Of File (EOF) input type problem.
This means you have to take the values of n and m unless the program reaches the end of the input file and you have to calculate and output the result at the same time.

Notice that the numbers of test cases is not specified in this problem and output is calculated for every input until the end. In the sample, there are two lines dedicating two values of n and m and so we will calculate and output the result 2 times for each inputs.

I have a difficulty in solving this problem. How do I take numbers as input until encountering EOF in C? Do I read it as a char type, float type or int type? I’ve gone through several articles in stack change but still understand nothing. Please help.

#include<bits/stdc++.h>

using namespace std ;

int main() {
long long int n , m , sum1 , sum2 ,sum ;
cin>> n >> m ;

``````if(m<n) { sum1 = (n*(n+1))/2 ;
sum2 = ((m-1)*(m-1+1))/2 ;

sum = sum1-sum2;

cout << "Sum of " << m << " to " << n << " is: " << sum <<";"  << endl ; }
else {
sum1 = ((n-1)*(n-1+1))/2 ;
sum2 = (m*(m+1))/2 ;
sum= sum2 - sum1 ;
cout << "Sum of " << n << " to " << m << " is: " << sum <<";"  << endl ;
}
``````

return 0 ;

}