Back to problems Sign in to unlock
Leetcode
Medium
Backtracking
Combinatorics
Recursion
Google
Combinations
Generate all size-k combinations of numbers from 1 to n.
Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
LeetCode Combinations
gfgProblem Statement
Given two integers n and k, return all possible combinations of k distinct numbers chosen from the range 1 to n.
Each combination should contain numbers in increasing order, and the collection of combinations may be returned in any order.
Input Format
- Two integers
nandk. - The valid numbers are
1, 2, ..., n. - Choose exactly
kdistinct numbers.
Output Format
- Return a list of all combinations.
- Each combination is a list of
kintegers in strictly increasing order. - The order of combinations in the final answer does not matter.
Constraints
1 <= k <= n1 <= n- The number of combinations can be large, so all valid results should be generated explicitly.
Examples
Sample cases returned by the problem API.
Example 1
Input
n = 4, k = 2
Output
[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Explanation
Choose any 2 distinct numbers from 1 through 4. Each valid pair appears once.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.