+ 1
Can someone tell me the error while writing 1 on right side in second and third for loop?
This question is asked in codeforces competition .The code I have written and the solution has only difference of 1 on left and right side of second and third for loop . Can someone please tell me the error in my code? The link for problem is https://codeforces.com/contest/1370/problem/B And codes are https://code.sololearn.com/cy3qd5pTbZIo/?ref=app https://code.sololearn.com/cVOVBrit664A/?ref=app
8 Réponses
0
Ren Grevart
Please can specify the what inputs you are giving to both the program while executing
Check inputs it depends on number of even and odd values you give as input. Both will give same behaviour for same input even and odd numbers count.
0
Sir input are
4
3
2 4 6 8 10 12
3
1 3 5 7 9 11
5
2 4 6 8 10 2 4 6 8 10
5
1 3 5 7 9 1 3 5 7 9
0
Ren Grevart
I found the spot of error.
In first for loop where you are trying to add in o (odd) and e(even) vector it's adding I (looping variable) rather then input x (read by cin).
Code:
if (x%2 == 0) {
e.push_back(I); //I should be x
} else {
o.push_back(I); //I should be x
}
Instead of pushing input x it is pushing value of iterator i
0
Sir but in question we have to print index that's why I am storing indexes
0
Ren Greyart
In 2nd and 3rd for loop condition check needs I < o.siz xxe() - 1 requires parenthasis () around (o.size()-1) or int l = o.size(); and condition needs I < (l -1)
This is required because of operator precedence and associativity between operators.
0
Sir I have tried that too. And +/- has higher precedence than < so that will not change anything.
0
Ren Greyart
It is problem of type conversion after computation
o.size() method always returns positive value that to unsigned
When it compared like bellow
i < o.size()-1
o.size()-1 is computes biggest unsigned number
When o.size() is 0 it enters the loop and zero size vector fails and execution terminates
If it compared like bellow
i+1 < o.size() it works fine
because I+1 becomes 1 when I is 0 and o.size() is 0 it computes to falsifying the condition and it works fine
This kind of problem can be identified by exception
Hope this will satisfy needs
0
I now understand sir, thank you for your time.