markdown 打印给定大小为n的数组中r元素的所有可能组合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 打印给定大小为n的数组中r元素的所有可能组合相关的知识,希望对你有一定的参考价值。

# Print all possible combinations of r elements in a given array of size n
Given an array of size n, generate and print all possible combinations of r elements in array. For example, if input array is {1, 2, 3, 4} and r is 2, then output should be {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4}.
Following are two methods to do this.

Method 1 (Fix Elements and Recur)
We create a temporary array ‘data[]’ which stores all outputs one by one. The idea is to start from first index (index = 0) in data[], one by one fix elements at this index and recur for remaining indexes. Let the input array be {1, 2, 3, 4, 5} and r be 3. We first fix 1 at index 0 in data[], then recur for remaining indexes, then we fix 2 at index 0 and recur. Finally, we fix 3 and recur for remaining indexes. When number of elements in data[] becomes equal to r (size of a combination), we print data[].
```
<?php 
// Program to print all  
// combination of size r 
// in an array of size n 
  
// The main function that 
// prints all combinations  
// of size r in arr[] of  
// size n. This function  
// mainly uses combinationUtil() 
function printCombination($arr,  
                          $n, $r) 
{ 
    // A temporary array to  
    // store all combination 
    // one by one 
    $data = array(); 
  
    // Print all combination  
    // using temprary array 'data[]' 
    combinationUtil($arr, $data, 0,  
                    $n - 1, 0, $r); 
} 
  
/* arr[] ---> Input Array 
data[] ---> Temporary array to 
            store current combination 
start & end ---> Staring and Ending 
                 indexes in arr[] 
index ---> Current index in data[] 
r ---> Size of a combination  
       to be printed */
function combinationUtil($arr, $data, $start,  
                         $end, $index, $r) 
                  
{ 
    // Current combination is ready  
    // to be printed, print it 
    if ($index == $r) 
    { 
        for ($j = 0; $j < $r; $j++) 
            echo $data[$j]; 
        echo "\n"; 
        return; 
    } 
  
    // replace index with all 
    // possible elements. The 
    // condition "end-i+1 >=  
    // r-index" makes sure that  
    // including one element at 
    // index will make a combination  
    // with remaining elements at  
    // remaining positions 
    for ($i = $start;  
         $i <= $end &&  
         $end - $i + 1 >= $r - $index; $i++) 
    { 
        $data[$index] = $arr[$i]; 
        combinationUtil($arr, $data, $i + 1,  
                        $end, $index + 1, $r); 
    } 
} 
  
// Driver Code 
$arr = array(1, 2, 3, 4, 5); 
$r = 3; 
$n = sizeof($arr); 
printCombination($arr, $n, $r); 
  
// This code is contributed by ajit 
?> 
```
Output:
```
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
```
How to handle duplicates?
Note that the above method doesn’t handle duplicates. For example, if input array is {1, 2, 1} and r is 2, then the program prints {1, 2} and {2, 1} as two different combinations. We can avoid duplicates by adding following two additional things to above code.
* Add code to sort the array before calling combinationUtil() in printCombination()
* Add following lines at the end of for loop in combinationUtil()
```
        // Since the elements are sorted, all occurrences of an element
        // must be together
        while (arr[i] == arr[i+1])
             i++; 
```
See this for an implementation that handles duplicates.

以上是关于markdown 打印给定大小为n的数组中r元素的所有可能组合的主要内容,如果未能解决你的问题,请参考以下文章

13常见算法数组元素的区间查找

如何用O(nlogn)或O(n)的时间复杂度在python中解决对和问题?

对给定数组的 XOR 查询

合并k个有序数组

从整数数组中找到大小为 K 的连续子数组,使得从 1 到 k 的附加元素永远不会低于零

给定数字 n,如何打印大小为 m 的所有子序列?