Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

108. Combinations

MediumDay 32 of 60Recursion & Backtracking

Given two integers n and k, return every way to choose k distinct numbers from the range 1 to n. A combination is unordered, so [1,2] and [2,1] count as the same one. The answer may be in any order.

Example 1
Input: n = 4, k = 2
Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Example 2
Input: n = 1, k = 1
Output: [[1]]
Constraints
  • 1 <= n <= 20
  • 1 <= k <= n
Hints
Frequently asked at: Amazon, Google, Adobe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min