map<int, int> required;// Map to store value of Array B in map Data Structure pair(i+1, A[i])
map<int, int> mb;// Map to store the values with count in the current window eg, pair(1, 4) means 1 occured 4 times in current window.
int ini=-1, tini =-1, minn =INT_MAX, tminn =INT_MAX;// ini(initial pointer) tracks the start location of window and minn stores minimum number of elements to be removed from A, t for temporary storage of these value
longlong threshold =0, thresholdPass =0;// threshold keeps track of the count of the elements to check the requirement as the B array, threshold pass is the requirement as of the B array
int n = A.size();
int m = B.size();
for(int i=0;i<m;i++){
mb[i+1]=0;// initialize mb with required elements as key and count as value all zero
required[i+1]= B[i];// storing required map according to B
thresholdPass+=B[i];// threshold pass storing count of the total elements required to achielve the goal
}
for(int i=0;i<n;i++){// iterating through every element of arr A
int c = A[i];// ith element stored in c
if(mb.find(c)!=mb.end()){// check if c is there in map mb that's checks if this element is relevent and contributing in requirement count
if(mb[c]>=required[c]){// if c is required the is the count more than & equal to required -> means the count is already fulfilled as requirement
if(A[tini]== c){// check if the first element in window is the same element as c -> then needs to reduce the window size and also the count
tminn++;// first add the current element before reducing the size -> simply adding will increase extra element count
mb[c]++;// inc the count
int p = tini;
for(p=tini;p<i;p++){// now looping from ini to the current position to decrease the window size
int x = A[p];
if(mb.find(x)!=mb.end()){// if the element is present in the current window count map(it will always be present in the map but still a check in case of error) -> not of much use but in case of exception
if(mb[x]>required[x]){// if the current element's count in the current window is more than required
mb[x]--;// decrease the count, remove the element
tminn--;// decrease the count of extra elements
continue;
}
else{// if the current element's count in the current window is not more than/equal to required then no more decrease the window size and break this loop
break;
}
}
}
tini = p;// we got our new start position of the new window
}
else{// if the first element in window is not the same element as c then just increment the count and window size
tminn++;// Just increase extra elements count
mb[c]++;// also adding it to the current window map
}
}
else{// if c is required the is the count but current window's this element's count is less than required
if(threshold==0){// if we found our first required element, initialising our two pointers here
tini = i;
tminn =0;
}
threshold++;// incrementing the current window threshold count as new element added was required
// tminn = i - tini +1;
mb[c]++;// incr the element count in curr window map
}
}
else{// if the element is not required
if(threshold==0)continue;// if the element is not required and window is not inotiated yet, then ignore this element
// tminn = i - tini +1;
tminn++;// but we need to add it in our window hoping next element is the required one
}
if(threshold == thresholdPass){// if the condition satisfyies the just checking if the new window size is less than prev one and then updating for the smallest window