Hey in this solution [Solution: 41904873 | CodeChef], you made a mistake in declaring the count array. Because, you declared count as  * (10^7) and not  * (10^7 + 1) (Arrays are indexed starting from zero! (Read the symbol “^” as power, not XOR, just clarifying). This solution will run just fine if you declare count =  * (10^7 + 1). Check my solution here [Solution: 41907954 | CodeChef]. Check Line 20 in your Error solution with Line 22 in my AC solution)
The reason why your second solution got accepted is that, though you did the same mistake of declaring the count array of size 10^7, you had exactly 10^6 values in the array and your for loop at the end of your code ran till 10^6.
Also, the limits were till 10^6, but you declared count array of size 10^7+1 (That’s not a mistake though! It worked just fine! I’m just letting you know to have a look at the constraints in the question to solve efficiently, so that your code doesn’t get TLE’d).