You are not logged in. Please login at www.codechef.com to post your questions!

×

First - Editorial

PROBLEM LINK:

Practice
Contest

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+(k-1)d)

Sum of even numbers (a=2,d=2) S1 = (k/2)(4+(k-1)2) S1 = (k/2)(2k+2) S1 = kk+k

Sum of odd numbers (a=1,d=2) S2 = (k/2)(2+(k-1)2) S2 = (k/2)(2k) S2 = kk

S1-S2 = 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 SOLUTION

Observations 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, 14:28

nurdtechie98's gravatar image

4★nurdtechie98
723
accept rate: 0%

edited 14 Sep, 18:18

admin's gravatar image

0★admin ♦♦
19.6k349497539


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,......n-1 .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,.....n-1 . and 1,3,...... .find the sum of both the A.P.s and perform the given operation . you will get answer -(n+1)/2

link

answered 15 Sep, 20:09

hbot_42's gravatar image

1★hbot_42
11
accept rate: 0%

edited 15 Sep, 20:23

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,119
×1,554
×38
×9
×6

question asked: 09 Sep, 14:28

question was seen: 643 times

last updated: 15 Sep, 20:23