Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Combination Sum

Medium Acceptance 75% Points 30

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.

Constraints
  • 1 = n = 20
  • 1 = arr[i] = 50
  • 1 = target = 100
Topics
Recursion
Companies
Microsoft
Added 14 Apr 2026 75% acceptance
Ln 1, Col 1Sign in to save your code
2 3 6 7
7
 
7 223