#K41067. Unique Combinations
Unique Combinations
Unique Combinations
Given an array of integers and an integer k, generate all combinations (in lexicographical order with respect to the array indices) of length k from the array. Note that duplicate numbers in different positions are considered distinct. If k is 0, output a single empty combination (i.e. an empty line). If k is greater than the number of elements in the array, output nothing.
The mathematical formulation can be expressed in LaTeX as:
\(\text{For an array } A = [a_1, a_2, \dots, a_n], \text{ find all combinations } C \subseteq \{1,2,\dots,n\} \text{ such that } |C| = k.\)
Each combination should preserve the order of appearance in the original array. For example, for A = [1, 2, 3, 4] and k = 2, the valid combinations are:
1 2 1 3 1 4 2 3 2 4 3 4
inputFormat
The first line of input contains two integers n and k separated by space, where n is the number of elements in the array and k is the length of combinations desired.
If n is greater than 0, the second line contains n space-separated integers representing the array. Otherwise, the second line is absent.
outputFormat
Output all possible combinations of length k from the given array. Each combination should be printed on a new line with its elements separated by a single space. If the combination is empty (i.e., when k is 0), print an empty line. If no combinations exist (for example, when k > n), produce no output.
## sample4 2
1 2 3 4
1 2
1 3
1 4
2 3
2 4
3 4
</p>