 # DIVIDING - Editorial

Author: Konstantin Sokol
Tester: Gerald Agapov
Editorialist: Tasnim Imran Sunny

Cakewalk

Simple Math

### PROBLEM:

Given a list of N integers check if their sum is the same as the sum of first N natural numbers.

### EXPLANATION:

The required stamp division is possible if:

C1 + … + CN = 1 + 2 + … + N

There’s a very common formula for the sum of first N natural numbers that is:
1 + 2 + … + N = N(N+1)/2*. (Proof)

So just compute the sum of all the numbers and check if the sum is N*(N+1)/2. Be careful of overflow, use 64 bit integers for computing the sum.

### AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here.

1 Like

Hi,

This was exactly what I did and I got AC in Python only…

Possibly, my C++ solution was overflowing although I was using ULL…

Was there any issue like this? Or any trick which would prevent overflow?

1 Like

Thank you to all for the comments, I switched my previous declaration of total to:

unsigned long long int total = (n*(n+1ULL)/2);

and got AC at last!! Link to AC solution in Practice: http://www.codechef.com/viewsolution/3641364

Thank you,

Bruno

I’ve got AC-https://www.codechef.com/viewsolution/8251313
would somebody tell me what is happening behind sum=0ULL ??
ull is typedef unsigned long long .

with every addition of number arr* just subtract i+1 from the total sum and check weather the sum finally comes to be zero or not if zero then print yes else print no.

Code
long long int sum=0;
for(i=0;i<n;i++)
{
sum+=arr*;
sum-=(i+1);
}

1 Like

Here it is: AC Python solution: http://www.codechef.com/viewsolution/3636519

And the incorrect C++ one: http://www.codechef.com/viewsolution/3635612

No overflow in my case. I infact use long long and got AC.

http://www.codechef.com/viewsolution/3634526

1 Like

@kuruma The mistake is that you take n as an int due to which overflow occurs in total variable

1 Like

Yes, but then I’m declaring total as ULL… won’t it “typecast” immediately?

You also have to take n as long long int cuz when you multiply two integers and if they overflow the result would be wrong. Consider multiplying 10^6 * 10^6, if you have stored 10^6 in int than the result would overflow. It doesn’t matter if you have declared the storing variable as ull.

total = n*(n-1)/2 is where i think an overflow is possible because the intermediate variable will be stored in integer. A get around for that is total = n;total =(total*(n-1))/2;

Maybe the problem is that n*(n+1)/2 is evaluated as int and then cast to long long?

The way you calculate total is incorrect : n*((n+1)/2) for n = 2 gives 2 instead of 3.

Other than overflow error, you got WA because you use
int total = n*((n+1)/2);

for n=4 , total=8
but it should be 10.

the formula should be int total = n*(n+1)/2; because associativity is from left to right and one out of n and n+1 will definitely be even.
so that n*(n+1) will be even…

1 Like

there is a problem in your c++ code … your are taking n to be int and when you compute n*(n+1)/2 it tries to fit the answer in int …which overflows… so try taking long long int for int and you will get ac …also you need to change the statement n*((n+1)/2) to (n*(n+1))/2… this will get you a ac

http://www.codechef.com/viewsolution/3636433
Whats wrong with that?

#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
unsigned long long int a[n],s=0,k,r,i;
for(i=0;i<n;i++){
cin>>a[i];
s+=a[i];
}
k=s;
r=(n*(n+1))/2;
if(r==k)
cout<<“YES”<<endl;
else
cout<<“NO”<<endl;
return 0;
}
why it is giving wrong answer??

but why do we need to substract (i+1) as we are checking if the (n*(n+1)/2)==sum of all elements of array

Can you please explain why do we substract (i+1) from the sum every time we add the no of cards