1

I want to print all combination of sub range in an given array. I have an array of y number of elements in it from which I want to print all combination of contiguous sub range.

Constraint is : each sub range should have at least 2 elements and each element in sub range should be contiguous. It should share same border of each element.

For example, We have an array of 7 elements [11,12,13,14,11,12,13]

So, the total number of sub range combination will [7 * (7-1) /2] = 21

So, the Output will be something like this:

11,12

12,13

13,14

14,11

11,12

12,13

11,12,13

12,13,14

13,14,11

...

11,12,13,14 and so on (total 21 combination as per above array)

we should not print any combination which is not contiguous. example: [11,12,14] is not valid combination as it skips the element "13" in between.

I am able to print the combination with 2 elements but i am having difficulty in printing more then 2 elements combination.

Below is what I have tried so far.

$data=array("11","12","13","14","11","12","13");
$totalCount=count($data);

for($i=0;$i<$totalCount;$i++){
    if(($i+1) < ($totalCount)){
        echo "[".$data[$i].",".$data[$i+1]."]<br>";
    }
}
georg
  • 211,518
  • 52
  • 313
  • 390
Vidit Mody
  • 45
  • 5
  • Possible duplicate of [PHP: How to get all possible combinations of 1D array?](http://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array) – devsdmf Mar 23 '17 at 18:09
  • @LucasMendes Its not duplicate. I want the combination to be contiguous . Like from the above example [11,12,14] is not valid combination as it is not not contiguous and skip the element "13" in between. element in each combination should share same border. – Vidit Mody Mar 23 '17 at 18:52
  • 1
    @ViditMody, in your example you are printing all the combinations. You should edit your question to include correct sample output. – TheGentleman Mar 23 '17 at 19:06

2 Answers2

2

You can do that:

$arr = [11,12,13,14,11,12,13];

function genComb($arr, $from = 1, $to = -1) {
    $arraySize = count($arr);
    if ($to == -1) $to = $arraySize;
    $sizeLimit = $to + 1;
    for ($i = $from; $i < $sizeLimit; $i++) { // size loop
        $indexLimit = $arraySize - $i + 1;
        for ($j = 0; $j < $indexLimit; $j++) { // position loop
            yield array_slice($arr, $j, $i);
        }
    }
}

$count = 0;
foreach (genComb($arr, 2) as $item) {
    echo implode(',', $item), PHP_EOL;
    $count++;
}

echo "total: $count\n";
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

Casimir et Hippolyte was faster, but you can gain huge performance by processing each contiguous section independently:

function getCombos(&$data) {
    $combos = array();
    $count = count($data);
    $i = 0;
    while ($i < $count) {
        $start = $i++;
        while ($i < $count && $data[$i - 1] + 1 == $data[$i]) // look for contiguous items
            $i++;
        if ($i - $start > 1) // only add if there are at least 2
            addCombos($data, $start, $i, $combos); // see other answer
    }
    return $combos;
}
Community
  • 1
  • 1
maraca
  • 8,468
  • 3
  • 23
  • 45
  • Thanks @maraca. You are calling addCombos() function. But where is that function defined. Also, your comment says see other answer. Can you please tell me which answer? Thanks – Vidit Mody Mar 23 '17 at 22:15