Skip to main content

Combination Sum

Definition​

The Combination Sum Algorithm finds all unique combinations of a given set of numbers that sum up to a specific target value. It's a backtracking algorithm commonly used in scenarios where you need to solve problems related to combinations and permutations

Practice​

combinationSum(nums, target):
sort(nums) // Sort the input array
result = [] // Initialize result list

backtrack(0, target, []) // Call the recursive function with initial parameters
return result

backtrack(start, target, path):
if target == 0:
result.append(path) // Add current combination to result
return

for i from start to length(nums):
if nums[i] > target:
break // Stop recursion if current element exceeds target

backtrack(i, target - nums[i], path + [nums[i]]) // Recur with updated target and path