Skip to content

Instantly share code, notes, and snippets.

@ashecret
Last active February 13, 2024 13:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashecret/5f61c1d0f8735c108ded8dc3110a120e to your computer and use it in GitHub Desktop.
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.
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;
@Y81Kakaei
Copy link

Hi Ashraf,
could you please elaborate on line 12? I don't understand the logic of this line of code,
tnx

@sanmai
Copy link

sanmai commented Jun 13, 2020

Imagine if we removed an element that was out of order. One way to proceed would be to remove that element and run over the array again from the start. Another way it to check if any nearby elements are out of order, which is what this last clause does.

@sanmai
Copy link

sanmai commented Jun 13, 2020

Note that in a matching sequence both every and every other elements must be increasing.

@cameravision
Copy link

For i = 0 what will be sequence[i-1]?

@Hcaz528
Copy link

Hcaz528 commented Feb 6, 2022

It would be undefined

@noorkhan-92
Copy link

It would be undefined

But in Java it throws ArrayIndexOutofBound Exception...?

@simpyparveen
Copy link

boolean solution(int[] sequence) {
// [1, 2, 1, 2]
// i-2, i-1, i, i+1

// Stores the count of numbers that are needed to be removed
int count = 0;

// Store the index of the element that needs to be removed
int index = -1;

// Traverse the range [1, N - 1]
for(int i = 1; i < sequence.length; i++)
{
     
    // If arr[i-1] is greater than or equal to arr[i]
    if (sequence[i - 1] >= sequence[i])
    {
         
        // Increment the count by 1
        count++;

        // Update index
        index = i;
    }
}

// If count is greater than one
if (count > 1)
    return false;

// If no element is removed
if (count == 0)
    return true;

// If only the last or the first element is removed
if (index == sequence.length - 1 || index == 1)
    return true;

// If a[index] is removed
if (sequence[index - 1] < sequence[index + 1])
    return true;

// If a[index - 1] is removed
if (index - 2 >= 0 && sequence[index - 2] < sequence[index])
    return true;
   
  // if there is no element to compare
  if(index < 0)
      return true;

return false;

}

@mirlabraga
Copy link

function solution(sequence) {
   let total = 0;
  let size = 0;
  let objectCount = new Array();
  const map = new Map();
  for (let i = 0; i < sequence.length; i++) {
    const result = sequence.filter(x => x == sequence[i]);
    if (result.length >= 2 && !objectCount.includes(sequence[i])) {
      objectCount.push(sequence[i]);
      total++;
    }
    if (sequence[i] >= sequence[i + 1]) {
      size++;
    }
    let max = 0;
    for (let j = i; j >= 0; j--) {
      if (sequence[j - 1] >= sequence[i]) {
        max++;
      }
    }
    map.set(sequence[i], max);

    if (total >= 2 || size > 1) {
      return false;
    } 
  }
  return Array.from(map.values()).filter(x => x >= 2).length < 2;
}

It's solution works, however for the test case is too large, it's not. Some suggestions?

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment