PROBLEM LINK:Author: Nishchith Shetty Tester: Karan Sheth Editorialist: Chirag Shetty DIFFICULTY:Easy PREREQUISITES:Simple Math PROBLEM:Given an integer n you have to print result adding all even nos till n and subtracting all odd nos tll n. EXPLANATION:Since n is very large adding even nos one by one and subtracting odd nos one by one will give you TLE. There are 2 simple cases. 1) If n is even (say 10) then answer is n/2 (i.e 5) If n is even then we can consider first n/2 odd numbers and and first n/2 even numbers excluding 0 as 0 won't add up to the sum. For eg if n=10, Odd nos  1 3 5 7 9 Even nos  2 4 6 8 10 (excluding 0) Let k = n/2 (i.e 5) Using sum of Arithmetic Progression Formula : S = (k/2)(2a+(k1)d) Sum of even numbers (a=2,d=2) S1 = (k/2)(4+(k1)2) S1 = (k/2)(2k+2) S1 = kk+k Sum of odd numbers (a=1,d=2) S2 = (k/2)(2+(k1)2) S2 = (k/2)(2k) S2 = kk S1S2 = k = n/2 2) If n is odd (say 9) then answer is (n+1)/2 (i.e 5) If we take n+1 then that is even and we just need to subtract the last even number i.e n+1 from above formula. i.e (n+1)/2(n+1) = (n+1)/2 ALTERNATIVE SOLUTIONObservations skills :P AUTHOR'S AND TESTER'S SOLUTIONS:Author's solution can be found here. Tester's solution can be found here.
This question is marked "community wiki".
asked 09 Sep '18, 14:28

Actually the solution is pretty simple . using some maths and properties of arithematic progressions i found out that if the number entered by the user is even then we just have to output the number entered by user divided by two . for example : if user enters 8 output 8/2 i.e. 4. the A.P. formed will be 2,4,.....n . and 1,3,......n1 .find the sum of both the A.P.s and perform the given operation . you will get answer n/2 on the other hand if number entered by user is odd then you have to check if the number is 1 or greater than 1 . if one you have to print 1 and if greater than 1 print (n+1)/2. the A.P. formed will be 2,4,.....n1 . and 1,3,...... .find the sum of both the A.P.s and perform the given operation . you will get answer (n+1)/2 answered 15 Sep '18, 20:09
