Last active
February 13, 2024 13:35
-
-
Save ashecret/5f61c1d0f8735c108ded8dc3110a120e to your computer and use it in GitHub Desktop.
CodeFight: 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function almostIncreasingSequence(sequence) { | |
var found = 0; | |
for (var i=0;i<sequence.length;i++) { | |
if(sequence[i] <= sequence[i-1]) { | |
found++; | |
// check if more than one nonincreasing found | |
if(found > 1) return false; | |
// check if second previous number is equal to / bigger than current number | |
// and previous number is equalto / bigger than next number | |
if(sequence[i] <= sequence[i-2] && sequence[i+1] <= sequence[i-1]) return false; | |
} | |
} | |
return true; | |
} | |
// [1, 3, 2, 1] returns false. | |
// [1, 3, 2] returns true. | |
// [1, 2, 1, 2] returns false. | |
// [1, 4, 10, 4, 2] returns false; | |
// [10, 1, 2, 3, 4, 5] returns true; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's solution works, however for the test case is too large, it's not. Some suggestions?
Thank you!