Combinations - aloalgo

Combinations

Medium

You are given an array of distinct integers nums and an integer k.

Your task is to return all possible combinations of k numbers chosen from nums.

You may assume that:

  • All integers in nums are distinct.
  • k <= nums.length

Return a list of all possible combinations. The order of combinations in the output does not matter, and the order of numbers within each combination does not matter.

Example 1

Inputs
nums = [1, 2, 3, 4]
k = 2
Output
[
  [1, 2],
  [1, 3],
  [1, 4],
  [2, 3],
  [2, 4],
  [3, 4]
]
Explanation:

There are C(4, 2) = 6 combinations of 2 numbers chosen from [1, 2, 3, 4].

Example 2

Inputs
nums = [5, 10, 15]
k = 2
Output
[
  [5, 10],
  [5, 15],
  [10, 15]
]
Explanation:

There are C(3, 2) = 3 combinations of 2 numbers chosen from [5, 10, 15].

Loading...
Inputs
nums = [1, 2, 3, 4]
k = 2
Output
[
  [1, 2],
  [1, 3],
  [1, 4],
  [2, 3],
  [2, 4],
  [3, 4]
]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!