Why this is giving tle?

this is the question
https://hack.codingblocks.com/app/practice/3/1001/problem

and this is my code
https://hack.codingblocks.com/app/practice/3/1001/problem

hello @dhruvilcodes

u forgot to add ur code link

Coding Blocks IDE

@dhruvilcodes
image

bro check the constraints .
u cant solve it in O(N) .
try to solve it in O(1)
HINT- > JUST SPERATE ALL NEGATIVE NUMBERS AND ALL POSITIVE NUMBERS AND USE AP FORMULA TO COMPUTE THE SUM OF BOTH SERIES IN o(1)

Coding Blocks IDE this is still giving wrong answer

@dhruvilcodes
it is becuase of overflow simplify it furthur.

write both formulas and cancel the ± terms.
and then calculate

Coding Blocks IDE I wrote both the sum together still it is giving wrong answer
but samplecase is right

no first simplify it on paper otherwise due to interger overflow u will keep geeting wrong answer.
for odd series if a terms are then then formula is -a^2

for even seried if b terms are there then forumula is b*(b+1)
sum= b^2+b-a^2

now we have n . we can break it in two case

if n is odd 
    here a=b+1  where b=n/2
    put in above formula
    b^2+b-(b+1)^2=> b^2+b-(b^2+1+2b)
                            => b^2 + b - b^2-1-2b
                           => -b -1 
                            => -n/2-1



if n is even
    it means a==b ==n/2 then putting in above formula we get
       b^2+b-a^2 => b  so simply print b (n/2) 


so u only have to print this simplied values

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.