MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Complete

Class Complete

MinimumSwapsAndKTogether.java:1–31  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Complete{
2 // Function for finding maximum and value pair
3 public static int minSwap (int arr[], int n, int k) {
4 //Complete the function
5
6 int fav=0,nonFav=0;
7 for(int i=0;i<n;i++)
8 {
9 if(arr[i]<=k) fav++;
10 }
11
12 for(int j=0;j<fav;j++)
13 {
14 if(arr[j]>k) nonFav++;
15 }
16
17 int l=0,r=fav-1, result = Integer.MAX_VALUE;
18
19 while(r<n)
20 {
21 result = Math.min(result,nonFav);
22 r++;
23 if(r<n && arr[r]>k) nonFav++;
24 if(l<n && arr[l]>k) nonFav--;
25 l++;
26 }
27
28 return (result == Integer.MAX_VALUE)?0:result;
29
30 }
31}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected