I am trying to solve the CodeSignal Arcade challenge almostIncreasingSequence.

The task is to find out if it is possible to get a strictly increasing sequence by removing no more than one element.

My code passed 17 out of 19 Test cases. I understand why one of those test cases failed. I do not understand why this one fails, though:

Input: sequence: [3, 5, 67, 98, 3] Output: false Expected Output: true Console Output: "Counter: 4"

I do not understand, why it returns Counters = 4.

boolean almostIncreasingSequence(int[] sequence) {
   int i, j;
   int counter = 0;

   for (i=0; i <= sequence.length-1; i++){
      for (j=i+1; j <= sequence.length-1; j++){
         if (sequence[i] >= sequence[j]){
            counter++;
            break;
         }
      }
   }

   System.out.println("Counter: " +counter);

   if (counter <= 1)
      return true;
   else
      return false;
}

1 Answers

0
Thomas Bitonti On

These loops are generating too many tests:

 for ( outerTrav = 0; outerTrav <= sequence.length-1; outerTrav++ ) {
     for ( innerTrav = outerTrav + 1; innerTrav <= sequence.length-1; innerTrav++ ) {

Those are generating comparisons for each element against all following elements. Shouldn't the comparisons be just each element against the immediately following element?