PROFDE Approach CodeStart

Hello everyone,
Could anyone suggest the right approach for solving the problem PROFDE in codestart (other than the naive approach)? I would appreciate, if anyone could suggest the right method for solving the problem (as I am finding it difficult to understand, where i have went wrong).

Problem statement : Link