import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    
    public static int minswap(int [] arr , int B){
        int n = arr.length;
        int countofGood = 0;
        int countofBad = 0 ;

        // Count of good element : ele <= B .. 
        for(int i=0; i<n ; i++){
            if(arr[i] <= B ){
                countofGood++;
            }
        }

        //Count of bad element : ele  > B  in the first window 
        // size of good element is the window size
        for(int i=0 ; i<countofGood ; i++){
            if(arr[i]>B){
                countofBad++;
            }
        }
        

        int ans = countofBad;
        //Shift the window 
        int s = 1 ;
        int e = countofGood;
        while(e<n){
            if(arr[e] > B ){
                countofBad++;
            }
            if(arr[s-1] > B ){
                countofBad--;
            }
            ans = Math.min(ans,countofBad);
            s++;
            e++;
        }
        
        
        return ans ;
    }
    
    public static void main(String[] args) {
        int[] A = {19, 11, 3, 9, 7, 25, 6, 20, 4};
        int B = 10;

        System.out.println("Minimum Swapping: " + minswap(A, B));
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: