31 · Backtracking

#77 Combinations

Mediumsolve on LeetCode ↗

frequently reported atMetaAmazonGoogleMicrosoft

the problem

In plain words

Given two integers n and k, return every way to pick k different numbers from 1 to n. A combination is a set: [1, 2] and [2, 1] are the same combination, so list it only once. Any order is fine.

in n = 4, k = 2

out [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]

C(4, 2) = 6 ways.

in n = 1, k = 1

out [[1]]

  • 1 ≤ n ≤ 20
  • 1 ≤ k ≤ n

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #78
      Subsets
      Mediumin Backtracking
    2. #39
      Combination Sum
      Mediumin Backtracking
    3. #46
      Permutations
      Mediumin Backtracking