Find number of max swaps then use the sliding window technique


#1

int Solution::solve(vector &A, int B) {
int smaller_or_equal = 0;
for(int val : A){
if(val <= B) smaller_or_equal++;
}
int miss_match = 0;
for(int i = 0; i < smaller_or_equal; ++i){
if(A[i] > B){
miss_match++;
}
}
int min_miss_matches = miss_match;
// keep a window in the size of smaller_or_equal;
int start = 0, end = smaller_or_equal;
while(end < A.size()){
if(A[start++] > B) miss_match–;
if(A[end++] > B) miss_match++;
min_miss_matches = min(min_miss_matches,miss_match);
}
return min_miss_matches;
}