 # Wrong ans in RRCOPY

Why I am getting wrong answer.

http://www.codechef.com/viewsolution/4367292

@vani57: you are initializing array ‘a’ only for ‘n’ indices, what if n=3 and numbers are in range some 10^4 to 10^5. when you are checking a == 0 it may not be 0 because you are initializing a to a = 0 so your program is going wrong. i hope you understand and if this is helpful to you upvote and accept my answer. Thank you happy coding 1 Like

just change the condition in the for loop …here is your corrected solution:http://www.codechef.com/viewsolution/4367791

It depends on the value on n

``````for(i=0;i<n;i++)
a[i]=0;
``````

suppose there are 2 test cases ,for first case n is large and for second case n is smaller than the first case
since the value on n is less in second test case,a[i] won’t be 0 for those values and hence the wrong answer
here is corrected version of your solution

http://www.codechef.com/viewsolution/4367706

but i must thank you for showing me a very simple way of solving this problem without sorting.i had used sorting and my time complexity was o(nlogn).

but there is a major drawback in your approach:your approach just got lucky here because 1<=Ai<=10^5 in this problem but if the constraint was like 1<=Ai<=10^10,then you will end up declaring an array of size 10^10 and in the process of initializing it you will end up doing 10^10 operations which will then have both tle and memory problems.So your code is an optimized approach under the given range but will not work if the constraints change…i think you were aware of this,in case not,this explanation must help you .And by the way if you find my answer helpful then upvote it.HAPPY CODING:)