Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function that recieves an array and returns if it can be made strictly increasing by removing only one of it's element

I’m currently working on a function as described in the tittle, where I need to get and array of X values and see if they get in a perfectly increasing order by removing one and only one of it’s elements, the problem is that this must be made in PHP, while the logic seems to be adding up, even tried this on another language successfully, but it doesn’t work on PHP probably due to my own limited knowledge, any help is appreciated.

<?php
function check($arr, $n){

    $modify = 0;

    if ($arr[0] > $arr[1]){
        $arr[0] = $arr[1] / 2;
        $modify++;
    }
 
    for ($i = 1; $i < $n - 1; $i++){
    
        if (($arr[i - 1] < $arr[i] && $arr[i + 1] < $arr[i]) 
        || ($arr[i - 1] > $arr[i] && $arr[i + 1] > $arr[i])){
            
            $arr[i] = ($arr[i - 1] + $arr[i + 1]) / 2;

            if ($arr[i] == $arr[i - 1] || $arr[i] == $arr[i + 1]){
                return false;
            }
 
            $modify++;
        }
    }
 
    if ($arr[$n - 1] < $arr[$n - 2]){
        $modify++;
    }
    
    if ($modify > 1){
        return false;
    }
    return true;
}
 
    $arr = array(1,3,2,1);
    $n = sizeof($arr);
    if (check($arr, $n) == true)
        echo "Yes";
    else
        echo "No";
?>

In this example the print should be No due to the input of an array with (1,3,2,1), but it prints Yes, by checking the code it simply doesn’t enter in any if or for of the funcion.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Its pretty simple, run the loop to check the sequence, if not in order then increase the counter. Check if the counter is greater than 1, then break the loop. Finally check the last the values applying same logic.

And you are not calling your function, check() is not defined.

function SequenciaCrescente($arr, $n){

    $modify = 0;
    
    for($i=0; $i<$n-1; $i++) {
      if($arr[$i] >= $arr[$i+1])
        $modify++;
        
      if($modify > 1)
        break;
    }
    
    if($arr[$n-2]>=$arr[$n-1])
      $modify++;    
    
    if($modify > 1)
      return false;
    
    return true;
}

$arr = array(1,3,2,4);
    $n = sizeof($arr);
    if (SequenciaCrescente($arr, $n) == true)
        echo "Yes";
    else
        echo "No";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading