I have given the following problem where I am stuck right now. I am not having the perfect logic for this. Here is the problem below :
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 1:
For sequence = [1, 3, 2, 1], the output should be false.
There is no one element that can be removed to get a strictly an increasing sequence
Example 2:
For sequence = [1, 3, 2], the output should be true.
You can remove 3 or 2 to get the strictly increasing sequence [1, 2] or [1,3] respectively
I have tried as follows :
boolean almostIncreasingSequence(int[] sequence) {
ArrayList<Integer> list = new ArrayList<>();
for(int i=0; i<sequence.length;i++) {
list.add(sequence[i]);
}
int omittedCounter = 0;
boolean status = true;
for(int i=1; i<list.size();i++) {
if(list.get(i-1) >= list.get(i)) {
omittedCounter ++;
list.remove(i-1);
break;
}
}
if (omittedCounter > 0) {
for(int i=1; i<list.size();i++) {
if(list.get(i-1) >= list.get(i)) {
omittedCounter ++;
list.remove(i-1);
break;
}
}
}
if (omittedCounter > 1) {
status = false;
}
return status;
}
But I am having trouble when I am given this sequence: [1, 2, 3, 4, 3, 6]
Output: false
Expected Output: true