what is the difference between (long long)(n * (n-1))and (long long)n * (n-1)/2… in the latter one i am getting correct answer whereas in the first one it is showing wrong answer.

@jatinagrawal31 Your code only gives the answer. In the second case n is first changed into long long from int and then multiplied. The result is easily accommodated in long long data type.

In the former case, the multiplication takes place first. Since n is still an int n * (n-1) exceeds the range of int and causes overflow. This overflowed wrong answer is then typecast to long long . This gives WA.

But when i declare n as long long still i am getting wrong answer…
http://www.codechef.com/viewsolution/3624436

But when i declare n as long long still i am getting wrong answer… http://www.codechef.com/viewsolution/3624436

But when i declare n as long long still i am getting wrong answer… http://www.codechef.com/viewsolution/3624436

The input format for Long Long int should be %lld i.e. for your variable n. Change it to that.

1 Like

thanks a ton

The problem was because:

1. You declared n as long long int and scan it using scanf("%d", &n);
2. In the statement for(int i = 0; i < n; i++) , i should be declared as long long int because it holds the values from 0 to n-1.

You can have a look at this edited solution of yours, which gives AC.
http://www.codechef.com/viewsolution/3633330

3 Likes
//