Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Return true if it is possible to remove one element from the array in order
- // to get a strictly increasing sequence, otherwise return false.
- // Sequence containing only one element is also considered to be strictly increasing.
- if (sequence.length <= 2) {
- return true;
- }
- int count = 0;
- // 1, 2, 3, 4, 99, 5, 6
- for (int i = 0; i < sequence.length - 1; i++) {
- if (sequence[i + 1] <= sequence[i]) {
- // remove one element from the array
- count++;
- if (i==0) {
- // directly skip, if it is the first element in the array
- // Example: {10, 1, 2, 3, 4, 5}
- continue;
- }
- boolean hasPrevious = i - 1 >= 0;
- boolean hasNext = i + 1 < sequence.length;
- // Example: { 1, 2, 3, 4, 99, 5, 6 }
- if (hasPrevious && hasNext && sequence[i+1] > sequence[i-1]) {
- continue;
- }
- // Example: 40, 50, 60, 10, 20, 30
- hasNext = i + 2 < sequence.length;
- if (hasNext && sequence[i + 2] <= sequence[i]) {
- return false;
- }
- }
- }
- return count <= 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement