Skip to content

Commit d0f0fcd

Browse files
author
Jakub Jirous
committed
6) make array consecutive
6) make array consecutive
1 parent b66c9b1 commit d0f0fcd

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ In preparation for technical interviews, I discovered this platform that provide
2020

2121
4) [Adjacent Elements Product](/src/arcade/intro/04-adjacent-elements-product/)
2222
5) [Shape Area](/src/arcade/intro/05-shape-area/)
23+
6) [Make Array Consecutive](/src/arcade/intro/06-make-array-consecutive/)
2324

2425

2526
---
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { makeArrayConsecutive } from "./make-array-consecutive";
2+
3+
type FirstArg = number[];
4+
type ExpectedResult = number;
5+
type Cases = [FirstArg, ExpectedResult][];
6+
7+
/**
8+
* Tests the minimal number of statues that need to be added to existing statues such that it contains every integer size from an interval [L, R] (for some L, R) and no other sizes.
9+
*/
10+
describe("Make Array Consecutive", () => {
11+
const cases: Cases = [
12+
[[6, 2, 3, 8], 3],
13+
[[0, 3], 2],
14+
[[5, 4, 6], 0],
15+
[[1], 0],
16+
];
17+
18+
test.each(cases)(
19+
"for statues = %s output should be %s",
20+
(firstArg, expectedResult) => {
21+
const result = makeArrayConsecutive(firstArg);
22+
expect(result).toEqual(expectedResult);
23+
}
24+
);
25+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size.
3+
* Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1.
4+
* He may need some additional statues to be able to accomplish that.
5+
* Help him figure out the minimum number of additional statues needed.
6+
*
7+
* Example:
8+
* - for statues = [6, 2, 3, 8], the output should be solution(statues) = 3
9+
* - Ratiorg needs statues of sizes 4, 5 and 7
10+
*
11+
* @param statues
12+
*/
13+
export const makeArrayConsecutive = (statues: number[]): number => {
14+
return Math.max(...statues) - Math.min(...statues) + 1 - statues.length;
15+
};

0 commit comments

Comments
 (0)