Skip to content

Commit 9124575

Browse files
author
Jakub Jirous
committed
7) almost increasing sequence
1 parent d0f0fcd commit 9124575

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ In preparation for technical interviews, I discovered this platform that provide
2121
4) [Adjacent Elements Product](/src/arcade/intro/04-adjacent-elements-product/)
2222
5) [Shape Area](/src/arcade/intro/05-shape-area/)
2323
6) [Make Array Consecutive](/src/arcade/intro/06-make-array-consecutive/)
24-
24+
7) [Almost Increasing Sequence](/src/arcade/07-almost-increasing-sequence/)
2525

2626
---
2727

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { almostIncreasingSequence } from "./almost-increasing-sequence";
2+
3+
type FirstArg = number[];
4+
type ExpectedResult = boolean;
5+
type Cases = [FirstArg, ExpectedResult][];
6+
7+
/**
8+
* Tests whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
9+
*/
10+
describe("Almost Increasing Sequence", () => {
11+
const cases: Cases = [
12+
[[1, 3, 2, 1], false],
13+
[[1, 3, 2], true],
14+
[[1, 2, 1, 2], false],
15+
[[3, 6, 5, 8, 10, 20, 15], false],
16+
[[1, 4, 10, 4, 2], false],
17+
[[10, 1, 2, 3, 4, 5], true],
18+
[[0, -2, 5, 6], true],
19+
[[1, 2, 3, 4, 5, 3, 5, 6], false],
20+
[[40, 50, 60, 10, 20, 30], false],
21+
[[1, 1], true],
22+
[[1, 2, 5, 3, 5], true],
23+
[[10, 1, 2, 3, 4, 5, 6, 1], false],
24+
[[1, 2, 3, 4, 3, 6], true],
25+
[[1, 2, 3, 4, 99, 5, 6], true],
26+
[[123, -17, -5, 1, 2, 3, 12, 43, 45], true],
27+
[[3, 5, 67, 98, 3], true],
28+
];
29+
30+
test.each(cases)(
31+
"for sequence = %s output should be %s",
32+
(firstArg, expectedResult) => {
33+
const result = almostIncreasingSequence(firstArg);
34+
expect(result).toEqual(expectedResult);
35+
}
36+
);
37+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
3+
*
4+
* Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an.
5+
* Sequence containing only one element is also considered to be strictly increasing.
6+
*
7+
* Example:
8+
* - For sequence = [1, 3, 2, 1], the output should be solution(sequence) = false.
9+
* - There is no one element in this array that can be removed in order to get a strictly increasing sequence.
10+
*
11+
* - For sequence = [1, 3, 2], the output should be solution(sequence) = true.
12+
* - You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].
13+
*
14+
* @param sequence
15+
*/
16+
export const almostIncreasingSequence = (sequence: number[]): boolean => {
17+
let sequenceInterruptions = 0;
18+
19+
for (let i = 1; i < sequence.length; i++) {
20+
if (sequence[i - 1] > sequence[i]) {
21+
sequenceInterruptions++;
22+
23+
if (sequenceInterruptions > 1) return false;
24+
25+
if (sequence[i - 1] >= sequence[i + 1] && sequence[i - 2] >= sequence[i])
26+
return false;
27+
}
28+
}
29+
30+
return true;
31+
};

0 commit comments

Comments
 (0)