Find number which is greater than or equal to N in an array

前端 未结 11 1038
长发绾君心
长发绾君心 2020-11-30 05:47

If I have a PHP array:

$array

With values:

45,41,40,39,37,31

And I have a variable:

$numb         


        
相关标签:
11条回答
  • 2020-11-30 06:40

    Here is a high-level process to get the desired results and work for any array data:

    • Filter the array keeping on values greater than or equal to the target and then select the lowest remaining value. This is the "best" value (which may be "nothing" if all the values were less) -- this is O(n)
    • Alternatively, sort the data first and see below -- this is O(n lg n) (hopefully)

    Now, assuming that the array is sorted ASCENDING, this approach would work:

    • Loop through the array and find the first element which is larger than or equal to the target -- this is O(n)

    And if the array is DESCENDING (as in the post), do as above, but either:

    • Iterate backwards -- this is O(n)
    • Sort it ASCENDING first (see fardjad's answer) -- this is O(n lg n) (hopefully)
    • Iterate forwards but keep a look-behind value (to remember "next highest" if the exact was skipped) -- this is O(n)

    Happy coding.

    0 讨论(0)
  • 2020-11-30 06:41

    If you really want the value that's "closest" in distance, even if it's a lesser value, try this, which @Jason gets most of the credit for.

    Imagine a scenario when you want the closest number to 38.9 in the following:

    $array = array(37.5, 38.5, 39.5);
    

    Most of the solutions here would give you 39.5, when 38.5 is much closer. This solution would only take the next highest value if what you're looking is in the exact middle between two numbers in the array:

    function nearest_value($value, $array) {
    if (array_search($value, $array)) {
        return $value;
    } else {
        $array[] = $value;
        sort($array);
        $key = array_search($value, $array);
        if ($key == 0) { return $array[$key+1]; }
        if ($key == sizeof($array)-1) { return $array[$key-1]; }
        $dist_to_ceil = $array[$key+1]-$value;
        $dist_to_floor = $value-$array[$key-1];
        if ($dist_to_ceil <= $dist_to_floor) {
            return $array[$key+1];
        } else {
            return $array[$key-1];
        }
    }
    }
    

    What it lacks in elegance, it makes up for in accuracy. Again, much thanks to @Jason.

    0 讨论(0)
  • 2020-11-30 06:41

    Here is my solution.

    $array=array(10,56,78,17,30);
    $num=65;
    $diff=$num;
    $min=$num;
    
    foreach($array as $a){
              if( abs($a-$num)< $diff ){
                  $diff=abs($a-$num);
                  $min=$a;
              }
    }
    
    echo $min;
    
    0 讨论(0)
  • 2020-11-30 06:42
    <?php
    function closest($array, $number) {
    
        sort($array);
        foreach ($array as $a) {
            if ($a >= $number) return $a;
        }
        return end($array); // or return NULL;
    }
    ?>
    
    0 讨论(0)
  • 2020-11-30 06:42

    Try this simple PHP function:

    <?php
    function nearest($number, $numbers) {
        $output = FALSE;
        $number = intval($number);
        if (is_array($numbers) && count($numbers) >= 1) {
            $NDat = array();
            foreach ($numbers as $n)
                $NDat[abs($number - $n)] = $n;
            ksort($NDat);
            $NDat   = array_values($NDat);
            $output = $NDat[0];
        }
        return $output;
    }
    
    echo nearest(90, array(0, 50, 89, 150, 200, 250));
    ?>
    
    0 讨论(0)
提交回复
热议问题