Problem in ANUBTG

When i Wrote the following code
http://www.codechef.com/viewsolution/6402087. I got WA ,
but changing the scope of vector from global to local
http://www.codechef.com/viewsolution/6402123
i got can anyone help??

You have declared the vector as global and its size is 1001 and initially all elements are 0. So when you sort it , you are actually sorting it from position 0 to positon 1000 even if you had taken 5 elements in the vector.

For example -

initally - arr = {0 ,0 ,0 , 0 …1000 times }

you take input 5 elements - 1 , 2 , 3, 4, 5

after input - arr = { 1 , 2 , 3, 4, 5 , 0 , 0 , 0 …995 times}

after sorting - arr = { 0 , 0 , 0 , … 1 ,2,3,4,5}

Now you are looping till nth (n=5) position. This causes all problems.

Whereas when you declared it locally , its size is n. and you take n elements in input. Here there are no extra zeros.

For example -

initally - arr = {0 ,0 ,0 , 0 …n times }

you take input 5 elements - 1 , 2 , 3, 4, 5

after input - arr = { 1 , 2 , 3, 4, 5}

after sorting - arr = {1 ,2,3,4,5}

Hope this helps.

Happy Coding !!!

1 Like

I,in my WA case,sorted the array in descending order.Then instead of
{0,0,0 …995 times,1,2,3,4,5}
my array would be {5,4,3,2,1,0,0,0…995 times}.

1 Like

ohh, sorry I didn’t notice rbegin and rend. I read it in hurry , and misinterpreted as begin and end.

1 Like

I got a problem in your solution. See the output section. http://ideone.com/RhCW17

1 Like