Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Algorithms/Greedy/max-min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @title Max Min
* @difficulty Median
* @link https://www.hackerrank.com/challenges/angry-children/problem
*/

const getUnfairness = (arr, length) => arr[length - 1] - arr[0];

function maxMin(subarrLength, arr) {
const {length: arrLength} = arr;
const sorted = arr.sort((a, b) => a - b);

let unfairness = Infinity;
for (let startIdx = 0; startIdx <= arrLength - subarrLength; startIdx++) {
unfairness = Math.min(unfairness, getUnfairness(sorted.slice(startIdx, startIdx + subarrLength), subarrLength));
}

return unfairness;
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Generates the README.md.
| --- | --- | --- |
| Easy | [2D Array - DS](https://www.hackerrank.com/challenges/2d-array/problem) | [Solution](./Interview-Preparation-Kit/Arrays/2d-array-ds.js)|
| Easy | [Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem) | [Solution](./Interview-Preparation-Kit/Arrays/left-rotation.js)|
#### Strings
| Difficulty | Problem | Solution |
| --- | --- | --- |
#### Warm-up-Challenges
| Difficulty | Problem | Solution |
| --- | --- | --- |
Expand All @@ -78,6 +81,10 @@ Generates the README.md.
| Difficulty | Problem | Solution |
| --- | --- | --- |
| Medium | [New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos/problem) | [Solution](./Algorithms/Constructive-Algorithms/new-year-chaos.js)|
#### Greedy
| Difficulty | Problem | Solution |
| --- | --- | --- |
| Median | [Fraudulent Activity Notifications](https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem) | [Solution](./Algorithms/Greedy/max-min.js)|
#### Sorting
| Difficulty | Problem | Solution |
| --- | --- | --- |
Expand Down