Last active
May 5, 2020 22:56
-
-
Save rakibulalam/b2e5ce55aee0c338df2f123cb70ee6b1 to your computer and use it in GitHub Desktop.
Max Min Hacker Rank
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function maxMin(k, arr) { | |
| const sortArray=arr.sort((a,b)=>a-b); | |
| const lengthArray=sortArray.length; | |
| let unfairValue=Infinity; | |
| for(let i=0; i<=lengthArray-k; i++) | |
| { | |
| const groupArray=sortArray.slice(i,i+k); | |
| const value=groupArray[k-1]-groupArray[0]; | |
| unfairValue=unfairValue<value?unfairValue:value; | |
| if(unfairValue===0)break; | |
| } | |
| return unfairValue; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment