Skip to content

Latest commit

 

History

History
48 lines (44 loc) · 2.47 KB

Almost_Increasing_Sequence.md

File metadata and controls

48 lines (44 loc) · 2.47 KB

🔷 Almost increasing sequence challenge:

Challenge description

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.

Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.

Example

  • For sequence = [1, 3, 2, 1], the output should be
    solution(sequence) = false.

    There is no one element in this array that can be removed in order to get a strictly increasing sequence.

  • For sequence = [1, 3, 2], the output should be
    solution(sequence) = true.

    You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

Input/Output

  • [execution time limit] 3 seconds (java)

  • [memory limit] 1 GB

  • [input] array.integer sequence

    Guaranteed constraints:
    2 ≤ sequence.length ≤ 105,
    -105 ≤ sequence[i] ≤ 105.

  • [output] boolean

    Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.

## Solutions:
  • JS solution

    function solution (inputArray){
    var toRemove = 0;
    for(let i = 0; i < inputArray.length - 1 && toRemove < 2; i++){
    const current = inputArray[i]
    const next = inputArray[i+1]
    if(current >= next){
    toRemove++
    if(i+2 < inputArray.length){
    const afterNext = inputArray[i+2]
    if( i === 0){
    if (next >= afterNext) {
    toRemove++
    }
    }else{
    const previous = inputArray[i-1]
    if(previous>= next && current >= afterNext){
    toRemove++
    }
    }
    }
    }
    }
    return toRemove < 2;
    }
    JS Execution

  • Java solution

    static boolean solution(int[] input) {
    int toRemove = 0;
    for(int i = 0; i < input.length-1 && toRemove < 2; i++){
    int current = input[i], next = input[i+1];
    if(current >= next){ // We need to remove at least one element (current, next, or both)
    toRemove++;
    if(i+2 == input.length){continue;} // if next is the last element only need to remove either current or next element
    int afterNext = input[i+2];
    if(i == 0){ // if current is the firts element check if the next two elements are consecutive
    /*
    If they are consecutive you only need to remove one element (current)
    Else you need to remove two elements (current and next)
    */
    if(next >= afterNext)
    toRemove++;
    }else{ // if current is not at first position verify if you also need to remove another element
    int previous = input[i-1];
    /*
    If current is greater or equal to afterNext then you need to remove current.
    If previous is greater or equal to next you need to remove next.
    If both are true then you need to remove both current and next
    */
    if(current >= afterNext && previous >= next)
    toRemove++;
    }
    }
    }
    return toRemove < 2;
    }
    Java Execution