Here is the problem link:
This is my code:

My idea is to store the starting and ending indices of each flavour and then try excluding each one of them. Excluding each of them by getting all the flavours from the beginning to the first occurrence of it and compare it with getting all the flavours from the ending to the last occurrence of it, the greater of these will be stored and compared to the greater of the other flavours.

Initially I tried to exclude the flavour who’s starting and ending indices were the closest, but I realized it was wrong as it failed in the below case:
10 4
1 2 1 2 1 2 4 3 3 1

So, I ended up excluding all of them but it still gives a WA.

Please point out the flaw in my logic and/or my code. Thanks in advance!

You are not checking if between the first and last index there is max answer. For eg-
6 3
1 2 3 2 2 1
O/p should be 4
You may see my code