Hi, this is a test from the website CodeFights, where you can test your coding skills. Unfortunately they don't have Pascal as a language to do the challenges, but I come to an interesting one that has been solved only by ~6000 people only in that website.
Personally at this time I've solved only 25 of 26 test cases. So is not solved for me.
A thing to take into consideration when trying to solve this is than you have a time limit of execution. In the case of JavaScript is 4000 ms, in the case of C++ is 500 ms, so for Pascal we can have something similar, how fast you can run this test in Pascal?
Hope you enjoy doing this test!
This is the actual test:
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.
Example
For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false;
There is no one element in this array that can be removed in order to get a strictly increasing sequence.
For sequence = [1, 3, 2], the output should be
almostIncreasingSequence(sequence) = true.
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].
Input/Output
[time limit] 4000ms (js)
[input] array.integer sequence
Guaranteed constraints:
2 ≤ sequence.length ≤ 10^5,
-10^5 ≤ sequence[ i ] ≤ 10^5.
[output] boolean
Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.
All test cases:
I buyed all test cases, here are them all:
Test1
Input:
sequence: [1, 3, 2, 1]
Output:
false
Expected Output:
false
Test2
Input:
sequence: [1, 3, 2]
Output:
true
Expected Output:
true
Test3:
Input:
sequence: [1, 2, 1, 2]
Output:
false
Expected Output:
false
Test4:
Input:
sequence: [1, 4, 10, 4, 2]
Output:
false
Expected Output:
false
Test5:
Input:
sequence: [10, 1, 2, 3, 4, 5]
Output:
true
Expected Output:
true
Test6:
Input:
sequence: [1, 1, 1, 2, 3]
Output:
false
Expected Output:
false
Test7:
Input:
sequence: [0, -2, 5, 6]
Output:
true
Expected Output:
true
Test8:
Input:
sequence: [1, 2, 3, 4, 5, 3, 5, 6]
Output:
false
Expected Output:
false
Test9:
Input:
sequence: [40, 50, 60, 10, 20, 30]
Output:
false
Expected Output:
false
Test10:
Input:
sequence: [1, 1]
Output:
true
Expected Output:
true
Test11:
Input:
sequence: [10, 1, 2, 3, 4, 5, 6, 1]
Output:
false
Expected Output:
false
Test12:
Input:
sequence: [1, 2, 3, 4, 3, 6]
Output:
true
Expected Output:
true
Test13:
Input:
sequence: [1, 2, 3, 4, 99, 5, 6]
Output:
true
Expected Output:
true
Test14:
Input:
sequence: [1, 1, 1]
Output:
false
Expected Output:
false
Test15:
Input:
sequence: [10, 12, 12, 12, 23]
Output:
false
Expected Output:
false
Test16:
Input:
sequence: [999, -987, -983, -972, -966, -934, -924, -917, -900, -898, -887, -879, -874, -867, -842, -804, -762, -729, -712, -703, -688, -677, -663, -661, -650, -628, -619, -610, -607, -599, -581, -578, -494, -488, -487, -477, -468, -461, -432, -381, -377, -376, -339, -330, -329, -304, -292, -291, -277, -257, -256, -242, -236, -235, -220, -137, -100, -46, -33, -17, -3, -2, 6, 35, 110, 124, 127, 130, 186, 214, 236, 255, 301, 311, 322, 348, 355, 374, 375, 384, 391, 392, 400, 410, 437, 487, 520, 572, 603, 627, 644, 657, 666, 676, 714, 750, 897, 898, 950, 972, 995]
Output:
true
Expected Output:
true
Test17:
Input:
sequence: [-997, -978, -975, -968, -959, -956, -907, -872, -871, -858, -836, -827, -823, -794, -786, -771, -740, -728, -716, -711, -697, -660, -659, -638, -633, -607, -601, -597, -573, -562, -548, -536, -517, -500, -433, -402, -387, -384, -301, -291, -283, -270, -252, -240, -230, -221, -215, -202, -200, -185, -171, -73, -47, -38, -34, -17, -3, 8, 42, 56, 65, 133, 140, 146, 180, 192, 201, 233, 241, 256, 285, 302, 320, 353, 324, 420, 428, 429, 442, 470, 487, 542, 547, 561, 564, 664, 666, 670, 693, 738, 793, 824, 845, 848, 864, 929, 931, 952, 985, 997, 1]
Output:
false
Expected Output:
false
Test18:
Input:
sequence: [1, 2, 3, 1]
Output:
true
Expected Output:
true
Test19:
Input:
sequence: [3, 4, 5, 10, 20, 10, 20, 30]
Output:
false
Expected Output:
false
Test20:
Input:
sequence: [5, 5, 5]
Output:
false
Expected Output:
false
Test21:
Input:
sequence: [-5, -4, -3, -2, 10, 2, 8]
Output:
true
Expected Output:
true
Test22:
Input:
sequence: [5384, 12, 34, 54, 48]
Output:
false
Expected Output:
false
Test23:
Input:
sequence: [1, 4, 5, 2, 3]
Output:
false
Expected Output:
false
Test24:
Input:
sequence: [1, 2, 3, 4, 5, 1, 2, 3, 4]
Output:
false
Expected Output:
false
Test25 and Test26 are stress tests, I will attach them because are too big to post them.