Minimum Swaps to Group
JavaScript
Medium
5 views
Problem Description
Given array and value k, you want all elements
Input Format
Line1: n k. Line2: n integers.
Output Format
One integer swaps.
Official Solution
const fs=require('fs');const a=fs.readFileSync(0,'utf8').trim().split(/\\s+/);if(!a[0])process.exit(0);let i=0;const n=Number(a[i++]);const k=Number(a[i++]);let arr=new Array(n);let good=0;for(let j=0;j<n;j++){arr[j]=Number(a[i++]);if(arr[j]<=k)good++;}if(good<=1){process.stdout.write('0');process.exit(0);}let bad=0;for(let j=0;j<good;j++)if(arr[j]>k)bad++;let ans=bad;for(let r=good;r<n;r++){if(arr[r-good]>k)bad--;if(arr[r]>k)bad++;if(bad<ans)ans=bad;}process.stdout.write(String(ans));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!