(This problem is from CodeFights, so I'm not entirely sure that I should be posting a solution here, but given that some people posted a solution to this problem in the comments I think I should be OK.)
The input to the code is an array of integers, and an almost increasing sequence is such an array that can be turned into a strictly increasing sequence of integers by removing 0 or 1 elements. The output should be true if the array is an almost increasing sequence, and false otherwise.
boolean almostIncreasingSequence(int[] sequence) {
boolean noFailuresYet = true;
for(int i = 0; i < sequence.length-1; i++) {
if(sequence[i] >= sequence[i+1]) {
if(noFailuresYet) {
if(i != 0 && i != sequence.length-2) {
if(sequence[i+1] <= sequence[i-1]) {
//Here we run the next iteration of the loop manually
//Alternatively we could set sequence[i] = sequence[i-1]
//but I don't want to modify the input array
//in case this function were to get used to check something elsewhere
if(sequence[i+2] <= sequence[i]) {
return false;
}
i++;
}
}
noFailuresYet = false;
} else {
return false;
}
}
}
return true;
}
What I don't like about this code is that it has a bunch of nested if statements.
After looking at a solution code, I saw that the condition in the 2 innermost if statements could be replaced by a local check that returns false immediately if there is a certain local pattern, but I would like to know if I can reduce the nested if statements without using that (unless there is a natural way to come up with that idea by thinking about improving the code).