 # CODEFORCES - Problem 318A Even Odds

My code : http://ideone.com/xnuKNK
Why is it not giving any output even for small test cases?

Second attempt (different code) : http://ideone.com/QuShGj
It gives incorrect output.
: http://codeforces.com/problemset/problem/318/A

There are too many things you are doing wrong.

1. You need not to declare array a.

2. You need not to use iteration and that surely gives TLE for input 10^12.

3. It’s a simple maths problem.

4. And not use lld or llu on codeforces since, codeforces don’t support them, and they surely give compile time error. Instead use %I64d.

Hint: It has O(1) solution.

1 Like

I do not agree with @sobhagya.

First of all practice is important, so writting code is good. Yes, it is brute force approach, but at least you have referential solution, that gives correct answers for smaller input.

The answer for your question “Why is it not giving any output?” is that your program is not terminating. The reason is your usage of for loop:

`````` for (x = 1, y = 1; x <= n - 1; x + 2)
``````

you have to observe, that x++ is not the same as x + 1, in fact it is x = x + 1, but in your for loop, in last part the assignment is missing, so value of x is not incrementing…

2 Likes

/**
The code is too just to provide with a simpler approach. Reader can take care of the input range
*/

``````int k ,n;
cout<<"enter k and n "<<endl;
cin >>k>>n;
int median= n/2;

if(k<median)
{
int rem=((2*k)-1);
cout<<rem<<endl;
}

else
{
int rem=n-median;
if (n%2==1)
rem--;

cout <<2*rem<<endl;
}``````
1 Like

In the first code as they already told ya x doesn’t increment in the for loop causing an infinite loop and consequently a TLE.

In the second code the approach itself is wrong. The first example gave Run Time Error because you defined the array a of size 100000 and u tried to read an entry beyond that.

What I meant by the approach itself is wrong is that, you are only saving odd entries in the array with odd numbers and the even entries are not being touch. the first loop gave u a = 7 so when u reached the print command it printed 7 as an answer which is wrong.

The idea behind the question is easy. Since we have n number and these numbers are divided into odd numbers and even numbers than we get n / 2 odd numbers & n / 2 even numbers if the n is even Or n / 2 + 1 odd numbers and n / 2 even numbers if n is even. Now if k is below or equal to (n + 1) / 2 then it is odd and the answer will be 2k - 1 otherwise it will be an even number at the entry k - (n + 1) / 2 thus the answer will be 2(k - (n + 1) / 2).

2 Likes

how do you get the formula?

how do you get the formula?,I dont understand how to get it and its the easy problem, It gives me TLE if I use a for loop

I know, practice is important but, even more important is following the right approach. Since, in contest programming it is quite necessary to have the right approach from the word go. And it is the problem which every newbie faces and start asking the questions i got TLE but on my machine it is working fine. And all that. And they stop participating in contest programming. And many of my friends faces the same issue.

So, giving right direction is much more important. And i know the question is “Why is it not giving any output?”. And i have to clarify that part also.

3 Likes

You have to consider two cases

Odd
if k is <= (n+1)/2 then, ans is 2k-1

Even
if k is above then, ans is 2(k - (n+1)/2)

1 Like

Thanks for answering I am new to programming contest, I need to practice more

Yes I think a hint can be very useful, because I tried this for an hour,and I dont get the formula until I checked the code of someone else