Can somebody explain me what wrong with this approach

Problem Link
Solution Link

if I get a valid subsegment from index l to r then it will also be sure that all subsequent between l to r is also valid.
so I make a dp array where dp[i] stores the starting index of valid subsegment till index i.
And then perform query.

I am unable to figure out what’s wrong with this approach or I miss something.
please help me to figure it out.

Your method is incorrect. It fails on the following test case.

5 1
1 2 1 1 2
3 5