Expected Change

Solution link : CodeChef: Practical coding for everyone
Problem link : CodeChef: Practical coding for everyone

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,
lld dp[i+1];
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 - CodeChef: Practical coding for everyone