Why am I getting TLE and WA in spite of using o(n) logic. My code is passing the first two subtests. Any help would be appreciated.
In line 80,
You have assigned i = length. If length = 10^7, it will give segmentation fault, because you need to allocate that memory from the heap.
There was the problem of overflow. The values of count and values in dp can exceed the range of long int so you have to take mod after each iteration.
Here is the link to your AC code - https://www.codechef.com/viewsolution/29797557