Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Combination Sum

Medium Acceptance 75.00% Points 30.00

You are given an array of distinct positive integers and a target value target. Your task is to find all unique combinations of elements whose sum equals the target.

You may use the same element multiple times. Return all valid combinations. The order of elements inside a combination does not matter.

Examples
Example 1

Example 1

Input: arr[] = [2], target = 4

Output: [2 2]

Explanation: The element 2 can be used twice to reach the target.

Example 2

Example 2

Input: arr[] = [2 3 6 7], target = 7

Output: [2 2 3] [7]

Explanation: Different combinations of the numbers add up to 7.

Example 3

Example 3

Input: arr[] = [2 3 5], target = 8

Output: [2 2 2 2] [2 3 3] [3 5]

Explanation: Multiple selections of the given numbers form different sums equal to 8.

Hints
Hint 1
N/A
Constraints
  • 1 = n = 20
  • 1 = arr[i] = 50
  • 1 = target = 100
Companies
Microsoft
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions