Can someone please help me figure out what’s wrong with my code?

Link to the question: https://codingcompetitions.withgoogle.com/codejamio/round/0000000000050fc5/0000000000054e9a

My approach:

Given an array, I first check with the first three elements of the array. If they form an increasing sequence, then I set the flag to 1 and if they form a decreasing sequence, I set the flag to -1. If they are all equal, then set the flag to 0. Initially, the flag is 2. Now, after examining the first three elements, if my flag is still 2, that means that I’ve found a desired sequence and hence, I increase the “count” (which is supposed to be the result that I print out as output at the end). In case, I find any increasing sequence, I keep examining the next elements of the array until I find an element which breaks this trend of monotonically increasingness. Once, such an element is found I can increment my count once again. Same applies to the case when I find a decreasing sequence of elements. This way I find out the number of desirable sequences and then the number of posts to be added is just gonna be count-1. Can someone please tell me what’s wrong with this.

My code:

#include

#include<bits/stdc++.h>

#include

#include

using namespace std;

int main()

{

int tc;

ios_base::sync_with_stdio(false);

cin.tie(NULL);

cout.tie(NULL);

cin >> tc;

for(int t=0;t<tc;t++)

{

int k;

cin >> k;

k++;

vector arr(k);

for(vector :: iterator it=arr.begin();it!=arr.end();it++)

cin >> *it;

```
int flag=2,count=0;
for(int i=0;i<k-2;i++)
{
if(flag==2)
{
if(arr[i+1]<arr[i+2] && arr[i+1]<arr[i])
{
count++; i=i+1; continue;
}
else if(arr[i+1]>arr[i+2] && arr[i+1]>arr[i])
{
count++; i=i+1 ;continue;
}
else if(arr[i]==arr[i+2])
flag=0;
else if(arr[i]<arr[i+2])
flag=1;
else if(arr[i]>arr[i+2])
flag=-1;
i=i+2;
}
else if(flag==0 && i>=2)
{
if(arr[i]>arr[i-1])
flag=1;
else if(arr[i]<arr[i-1])
flag=-1;
}
else if(flag==1 && i>=2)
{
if(arr[i]<arr[i-1])
{
count++;
flag=2; i=i-1; continue;
}
}
else if(flag==-1 && i>=2)
{
if(arr[i]>arr[i-1])
{
count++;
flag=2; i=i-1; continue;
}
}
}
cout << "Case #" << t+1 << ": " << count-1 << endl;
}
return 0;
```

}