Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

77. Combinations

Medium71.7% Acceptance
Backtracking
Asked by:
F
Facebook
A
Adobe
ProblemSolutions (12)VideosCompanies (8)Notes

Problem Statement

Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].

You may return the answer in any order.

Example 1:

Input: n = 4, k = 2
Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Explanation: There are 4 choose 2 = 6 total combinations.
Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.

Example 2:

Input: n = 1, k = 1
Output: [[1]]
Explanation: There is 1 choose 1 = 1 total combination.

Constraints:

  • 1 <= n <= 20
  • 1 <= k <= n
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Apple
Y
Yahoo
G
Google
+3

Approach

The #77 Combinations problem asks you to generate all possible combinations of k numbers chosen from the range 1..n. The most effective strategy is backtracking, a technique that incrementally builds candidates and explores all valid possibilities.

Start with an empty path and recursively add the next possible number. At each step, choose a number, append it to the current combination, and continue exploring until the combination size reaches k. Once it does, record the combination and backtrack by removing the last element to explore other choices.

An important optimization is pruning the recursion when there are not enough remaining numbers left to reach size k. This avoids unnecessary exploration and improves performance.

The backtracking tree explores all valid selections of k elements from n, resulting in roughly C(n, k) combinations. The approach is efficient for generating combinations while maintaining manageable recursion depth.

Complexity

ApproachTime ComplexitySpace Complexity
BacktrackingO(C(n, k) * k)O(k) recursion depth (excluding output)

Video Solution Available

Ashish Pratap Singh

View all video solutions

Solutions (12)

Backtracking Approach

This method uses a backtracking technique, where we recursively build each combination by adding numbers incrementally and backtrack whenever we've constructed combinations of size k or cannot extend further.

Time Complexity: O(C(n, k) * k), where C(n, k) is the binomial coefficient. Each combination is built in O(k) time.
Space Complexity: O(k) due to the recursion stack storing the current combination.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4void backtrack(int n, int k, int start, int* current, int curr_size, int** result, int* returnSize, int* returnColumnSizes) {
5    if (curr_size == k) {
6        result[*returnSize] = (int*) malloc(k * sizeof(int));
7        for (int i = 0; i < k; ++i) {
8            result[*returnSize][i] = current[i];
9        }
10        returnColumnSizes[*returnSize] = k;
11        (*returnSize)++;
12        return;
13    }
14    for (int i = start; i <= n; ++i) {
15        current[curr_size] = i;
16        backtrack(n, k, i + 1, current, curr_size + 1, result, returnSize, returnColumnSizes);
17    }
18}
19
20int** combine(int n, int k, int* returnSize, int** returnColumnSizes) {
21    int max_combinations = 1;
22    for (int i = 0; i < k; i++) {
23        max_combinations *= (n - i) / (i + 1);
24    }
25    int** result = (int**) malloc(max_combinations * sizeof(int*));
26    int* current = (int*) malloc(k * sizeof(int));
27    *returnSize = 0;
28    *returnColumnSizes = (int*) malloc(max_combinations * sizeof(int));
29    backtrack(n, k, 1, current, 0, result, returnSize, *returnColumnSizes);
30    free(current);
31    return result;
32}
33
34int main() {
35    int returnSize;
36    int* returnColumnSizes;
37    int n = 4, k = 2;
38    int** combinations = combine(n, k, &returnSize, &returnColumnSizes);
39    for (int i = 0; i < returnSize; i++) {
40        printf("[");
41        for (int j = 0; j < returnColumnSizes[i]; j++) {
42            printf("%d", combinations[i][j]);
43            if (j < returnColumnSizes[i] - 1) printf(", ");
44        }
45        printf("]\n");
46        free(combinations[i]);
47    }
48    free(combinations);
49    free(returnColumnSizes);
50    return 0;
51}

Explanation

This solution involves a recursive function backtrack that constructs combinations by adding candidates one by one and proceeds only if the current set can lead to a combination, which ensures our search tree doesn't explore invalid paths. Once a combination of size k is completed, it's stored in the result set.

Iterative Approach Using Bit Masking

Bit masking provides a novel way to generate combinations by representing choice decisions (e.g., pick or skip) in binary. A bit mask is applied to the set of numbers [1...n], and for each bit position, if the bit is set, that number is included in the combination.

Time Complexity: O(2n * n), as we iterate over all possible subsets of [1...n] and check the bit count.
Space Complexity: O(C(n, k) * k) for storing all combinations.

CC++JavaPythonC#JavaScript
1#


Video Solutions

Watch expert explanations and walkthroughs

LeetCode was HARD until I Learned these 15 Patterns

Ashish Pratap Singh
13:001,002,116 views

Asked By Companies

8 companies
F
Facebook
A
Adobe
A
Apple
Y
Yahoo
G
Google
B
Bloomberg
A
Amazon
M
Microsoft

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Letter Combinations of a Phone NumberMedium
Generate ParenthesesMedium
Sudoku SolverHard
Combination SumMedium
More similar problems

Related Topics

Backtracking

Problem Stats

Acceptance Rate71.7%
DifficultyMedium
Companies8

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

What is the optimal approach for Combinations?

The optimal approach for the Combinations problem is backtracking. It builds combinations step by step and explores all valid selections while pruning branches that cannot reach size k. This ensures we only generate valid combinations without unnecessary computation.

What data structure is best for solving Combinations?

A dynamic list or array is typically used to maintain the current combination while exploring recursion. The final results are stored in a list of lists. Recursion and backtracking manage the exploration of possibilities efficiently.

Is the Combinations problem asked in FAANG interviews?

Yes, combination generation problems frequently appear in technical interviews, including FAANG-style interviews. They test understanding of recursion, backtracking, pruning, and combinatorial thinking.

Can the Combinations problem be solved without backtracking?

Yes, combinations can also be generated using iterative techniques or bitmasking. However, backtracking is the most intuitive and commonly used approach because it naturally models the decision tree of choosing elements.

Previous Problem

N-Queens II

Next Problem

Subsets

include
<stdio.h>
2
#
include
<stdlib.h>
3
#
include
<string.h>
4
5
int
*
*
combine
(
int
n
,
int
k
,
int
*
returnSize
,
int
*
*
returnColumnSizes
)
{
6
int
total
=
1
<<
n
;
7
*
returnSize
=
0
;
8
int
*
*
result
=
(
int
*
*
)
malloc
(
1000
*
sizeof
(
int
*
)
)
;
9
*
returnColumnSizes
=
(
int
*
)
malloc
(
1000
*
sizeof
(
int
)
)
;
10
11
for
(
int
mask
=
0
;
mask
<
total
;
++
mask
)
{
12
if
(
__builtin_popcount
(
mask
)
==
k
)
{
13
result
[
*
returnSize
]
=
(
int
*
)
malloc
(
k
*
sizeof
(
int
)
)
;
14
int
idx
=
0
;
15
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
16
if
(
mask
&
(
1
<<
i
)
)
{
17
result
[
*
returnSize
]
[
idx
++
]
=
i
+
1
;
18
}
19
}
20
(
*
returnColumnSizes
)
[
*
returnSize
]
=
k
;
21
(
*
returnSize
)
++
;
22
}
23
}
24
return
result
;
25
}
26
27
int
main
(
)
{
28
int
returnSize
;
29
int
*
returnColumnSizes
;
30
int
n
=
4
,
k
=
2
;
31
int
*
*
combinations
=
combine
(
n
,
k
,
&
returnSize
,
&
returnColumnSizes
)
;
32
for
(
int
i
=
0
;
i
<
returnSize
;
i
++
)
{
33
printf
(
"["
)
;
34
for
(
int
j
=
0
;
j
<
returnColumnSizes
[
i
]
;
j
++
)
{
35
printf
(
"%d"
,
combinations
[
i
]
[
j
]
)
;
36
if
(
j
<
returnColumnSizes
[
i
]
-
1
)
{
37
printf
(
", "
)
;
38
}
39
}
40
printf
(
"]\n"
)
;
41
free
(
combinations
[
i
]
)
;
42
}
43
free
(
combinations
)
;
44
free
(
returnColumnSizes
)
;
45
return
0
;
46
}

Explanation

This code generates all possible bit masks for integers from 0 to 2n-1, checking for those that have exactly k bits set. The function __builtin_popcount is used to count the number of set bits in the mask, indicating how many items in the set are chosen for a combination.