Output is coming correct but test cases are not passing?

https://hack.codingblocks.com/contests/c/255/68
here is the code:


you have to change int->long long int because ā€œnā€ can be as large as 10^18 .
Plus this code will give you TLE.
Try to do it using maths.

1 Like

image
i have tried this method it also gives output but test case fails .
how to solve this using maths??

please save your code on ide and share the link .

https://ide.codingblocks.com/s/44223

Sum of first two terms = 1 Term->(o to 1)
Sum of next two terms = 1 Term->(2 to 3)
Sum of next two terms =1 Term->(4 to 5)
Sum of next two terms =1 Term->(6 to 7)

If n is even
so ans = (n/2)

Use this approach to find for odd value of n too.

if n is even ans=n/2 this is fine i did not understand first 4 lines

Hey Sarita, its just the explanation for how you are computing ans = n/2 when n is even. As shown in those above 4 lines when n is even, sum of every two terms i.e (i) + (i+1) is 1, so in this way you are getting n/2 times the value 1 which results in sum of n terms (n is even) = 1*n/2 => n/2.

2 Likes