int isP(int m,vector<int>&t,int k){//recieved mid vector of indeces of current element and k
int x=0;
for(int i=1;i<m;i++){//checking whether the answer is possible for window size m or mid
x+=t[i]-t[i-1]-1;
}
if(x<=k)//means answer is possible for the first window size itself hence no need to go further
return1;
//if we are not able to get the answer in the first window then we silmply use sliding window to move our window of size m forward
for(int i=m;i<t.size();i++){
x+=t[i]-t[i-1]-1;
x-=t[i-m+1]-t[i-m]-1;//sliding the window
if(x<=k)//if found a window then return 1
return1;
}
return0;
}
void solve(){
// int n;cin>>n;
// int k;cin>>k;
// vector<int>v(n);
int n=8,k=3;
vector<int> v={1, -2, 1, 1, 3, 2, 1 ,-2};
map<int,vector<int>>mp;
//storing all the position of indices of each element in the vector corresponding to that element
for(int i=0;i<n;i++){
// cin>>v[i];
mp[v[i]].push_back(i);
}
int fans =0;
//iterating through the keys of the map
for(auto x:mp){
vector<int>t=x.second;//storing the vector containing all the positons(indeces) of the current element in vector t
//Applying Binary Search to to check that whether we are able to get a valid answer for the current window size (mid)
int l=0;
int h=t.size();
int ans =0;
while(l<=h){
int mid =(l+h)/2;
int x = isP(mid,t,k);
if(x==1)//if answer is valid for the current window size mid(or m) then it might be the case that it is possible for larger size so we increase mid to get a larger window
{
ans = mid;
l=mid+1;//continuing further to obtain a better answer or larger window(if possible)
}
else{
h=mid-1;//else we decrease the window size which we are checking