Weird behaviour of C++ code during submission

Hi Community,
Recently i participated in ‘Programmers Army Monthly Contest - Bit Manipulation’ contest (https://www.codechef.com/BITM2021?itm_campaign=contest_listing). I was solving ’ Help Bob!’ problem (https://www.codechef.com/problems/HBOB02) and i faced weird issue. I was confident during the contest that my solution is correct. However, it kept failing due to wrong answer.

Later after the contest when i tried to debug my code i found out it is getting accepted if i am using ‘vector’ and failing while using ‘array’, rest everything is exactly same. Can someone please help me why is this so ? Am i doing something wrong here ?

Submission link using ‘array’ - https://www.codechef.com/viewsolution/42147645

Submission link using ‘vector’ - https://www.codechef.com/viewsolution/42147651

@admin @ashishgup

1 Like

4 millions bytes is probably too much for the stack.

1 Like

@ssjgz Is performance of dynamic array better than static one ? I used to believe other way round.

what about this Solution: 42134210 | CodeChef I haven’t any container still got WA.

Your (input) array solution is not working because you have not initialised the array, Though it should have not been a problem as every element till N is given a value (by input), unless there is some problem with test cases. I have tried with submitting without initialisation for both array and vector, and it doesn’t work, and it works after initializing using any value.

@keshav_7 If such is the case, it will be interesting to find root cause.

@keshav_7 @sunil5798 That is strange. Initialisation of array should not mandatory if we have input size.
You rightly said there could be a problem with test case.
One case scenario i can think of - The number of elements of array in the inputs is not equal to N.
Eg.) 5 2
2 3 4 5

Original array - “2 1 7 1 0” (for vector) and “2 1 7 1 random” ( for uninitialized array)
Correct output - 2

With Vector Solution - 5th element will be 0. So, Output = 2.
With Array Solution - 5th element can be any random number. As, array is not initialised. If original array’s 5th element ( = 5 XOR random number ) has 2nd set bit. Output = 3. Hence, a wrong answer.

@rashig123 - Can you please look into it ?