Loading problem...
You are given two positive integers: n representing the upper bound of a range of consecutive integers starting from 1, and k representing the desired subset size. Your task is to generate and return all possible unique subsets of exactly k distinct elements chosen from the integers in the range [1, n].
Each subset represents a selection of k numbers where the order of elements within the subset does not matter—that is, [1, 3] and [3, 1] are considered the same subset and should only appear once in your result.
The final result may be returned in any order. There is no requirement for the subsets themselves to be sorted internally, nor for the list of subsets to be in any particular sequence.
n = 4
k = 2[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]From the range [1, 2, 3, 4], we need to select all possible subsets containing exactly 2 elements. The total count follows the mathematical formula 'n choose k' = 4C2 = 6. Each pair represents a unique unordered selection. For instance, [1,2] means selecting elements 1 and 2 together. Note that [2,1] is not listed separately because it represents the same selection as [1,2].
n = 1
k = 1[[1]]The range contains only the integer 1, and we need subsets of size 1. There is exactly one such subset: [1]. Using the formula, 1C1 = 1.
n = 3
k = 2[[1,2],[1,3],[2,3]]From the range [1, 2, 3], we select all pairs of 2 elements. The total is 3C2 = 3 subsets. Each subset contains a distinct pair: (1,2), (1,3), and (2,3). These represent all ways to choose 2 items from 3 without repetition and without regard to order.
Constraints