[ create a new paste ] login | about

Link: http://codepad.org/PpuhcCvG    [ raw code | output | fork ]

PHP, pasted on Oct 24:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
$n = 2;
$count_arr = array();
foreach ($arr as $v) {
    $range = range(($v-$n),($v+$n)); // simple range between lower and upper bound
    $count = count(array_intersect($arr,$range)); // count intersect array
    $count_arr[] = ($count > 0 ? $count-1 : 0 );
}
print_r($arr);
print_r($count_arr);

?>


Output:
Array
(
    [0] => 0
    [1] => 0
    [2] => 1
    [3] => 2
    [4] => 2
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 7
    [9] => 9
    [10] => 10
    [11] => 10
)
Array
(
    [0] => 4
    [1] => 4
    [2] => 4
    [3] => 4
    [4] => 4
    [5] => 3
    [6] => 3
    [7] => 4
    [8] => 4
    [9] => 4
    [10] => 2
    [11] => 2
)


Create a new paste based on this one


Comments: