diff --git a/Interview-Preparation-Kit/Sorting/mark-and-toys.js b/Interview-Preparation-Kit/Sorting/mark-and-toys.js new file mode 100644 index 0000000..b27168c --- /dev/null +++ b/Interview-Preparation-Kit/Sorting/mark-and-toys.js @@ -0,0 +1,21 @@ +/** + * @title Mark and Toys + * @difficulty Easy + * @link https://www.hackerrank.com/challenges/mark-and-toys/problem + */ + +const ascendingCompare = (a, b) => a - b; + +function maximumToys(prices, budget) { + const sorted = prices.sort(ascendingCompare); + const {length} = sorted; + let sumPrice = 0; + for (let idx = 0; idx < length; idx++) { + sumPrice += sorted[idx]; + if (sumPrice > budget) { + return idx; + } + } + + return length; +} diff --git a/README.md b/README.md index 3b52e0b..bd7b7ec 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,10 @@ 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)| +#### Sorting +| Difficulty | Problem | Solution | +| --- | --- | --- | +| Easy | [Mark and Toys](https://www.hackerrank.com/challenges/mark-and-toys/problem) | [Solution](./Interview-Preparation-Kit/Sorting/mark-and-toys.js)| #### Dictionary-and-Hashmap | Difficulty | Problem | Solution | | --- | --- | --- |