Combination Sum – 1 - Leetcode - Tutorial (2023)

December 9, 2021 Arrays/Data Structure/Recursion

Problem Statement:

Given an array of distinct integers and atarget, you have to returnthe list of all unique combinations where the chosen numbers sum totarget.You may return the combinations in any order.

The same number may be chosen from the given array an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

It is guaranteed that the number of unique combinations that sum up totargetis less than150combinations for the given input.


Example 1:Input:array = [2,3,6,7], target = 7Output:[[2,2,3],[7]]Explanation:2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 7 is a candidate, and 7 = 7. These are the only two combinations.Example 2:Input:array = [2], target = 1Output:[]Explanation:No combination is possible.


Disclaimer:Don’t jump directly to the solution, try it out yourself first.

Solution: Recursion


For questions like printing combinations or subsequences, the first thing that should strike your mind is recursion.

How to think recursively?

Whenever the problem is related to picking up elements from an array to form a combination, start thinking about the“pick and non-pick”approach.


Defining recursive function:

Combination Sum – 1 - Leetcode - Tutorial (1)

Initially, the index will be 0, target as given and the data structure(vector or list) will be empty

Now there are 2 options viz topick or not pickthe current index element.

If youpickthe element, again come back at the same index as multiple occurrences of the same element is possible so the target reduces to target – arr[index] (where target -arr[index]>=0)and also insert the current element into the data structure.

If you decidenot to pickthe current element, move on to the next index and the target value stays as it is. Also, the current element is not inserted into the data structure.

While backtracking makes sure to pop the last element as shown in the recursion tree below.

Keep on repeating this process while index < size of the array for a particular recursion call.

You can also stop the recursion when the target value is 0, but here a generalized version without adding too many conditions is considered.

Using this approach, we can get all the combinations.

Base condition

If index== size of array and target == 0 include the combination in our answer

Diagrammatic representation for Example 1:

Case 1:

Combination Sum – 1 - Leetcode - Tutorial (2)

Case 2:

Combination Sum – 1 - Leetcode - Tutorial (3)


C++ Code

#includeusing namespace std;class Solution { public: void findCombination(int ind, int target, vector < int > & arr, vector < vector < int >> & ans, vector < int > & ds) { if (ind == arr.size()) { if (target == 0) { ans.push_back(ds); } return; } // pick up the element if (arr[ind] <= target) { ds.push_back(arr[ind]); findCombination(ind, target - arr[ind], arr, ans, ds); ds.pop_back(); } findCombination(ind + 1, target, arr, ans, ds); } public: vector < vector < int >> combinationSum(vector < int > & candidates, int target) { vector < vector < int >> ans; vector < int > ds; findCombination(0, target, candidates, ans, ds); return ans; }};int main() { Solution obj; vector < int > v {2,3,6,7}; int target = 7; vector < vector < int >> ans = obj.combinationSum(v, target); cout << "Combinations are: " << endl; for (int i = 0; i < ans.size(); i++) { for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << " "; cout << endl; }}


Combinations are:
2 2 3

Time Complexity: O(2^t * k)where t is the target, k is the average length

Reason:Assume if you were not allowed to pick a single element multiple times, every element will have a couple of options: pick or not pick which is 2^n different recursion calls, also assuming that the average length of every combination generated is k. (to put length k data structure into another data structure)

Why not (2^n) but (2^t) (where n is the size of an array)?

Assume that there is 1 and the target you want to reach is 10 so 10 times you can “pick or not pick” an element.

Space Complexity: O(k*x), k is the average length and x is the no. of combinations

Java Code

import*;import java.util.*;class Solution { private void findCombinations(int ind, int[] arr, int target, List < List < Integer >> ans, List < Integer > ds) { if (ind == arr.length) { if (target == 0) { ans.add(new ArrayList < > (ds)); } return; } if (arr[ind] <= target) { ds.add(arr[ind]); findCombinations(ind, arr, target - arr[ind], ans, ds); ds.remove(ds.size() - 1); } findCombinations(ind + 1, arr, target, ans, ds); } public List < List < Integer >> combinationSum(int[] candidates, int target) { List < List < Integer >> ans = new ArrayList < > (); findCombinations(0, candidates, target, ans, new ArrayList < > ()); return ans; }}public class Main { public static void main(String[] args) { int arr[] = {2,3,6,7}; int target = 7; Solution sol = new Solution(); List < List < Integer >> ls = sol.combinationSum(arr, target); System.out.println("Combinations are: "); for (int i = 0; i < ls.size(); i++) { for (int j = 0; j < ls.get(i).size(); j++) { System.out.print(ls.get(i).get(j) + " "); } System.out.println(); } }}


Combinations are:
2 2 3

Time Complexity: O(2^t * k)where t is the target, k is the average length

Reason:Assume if you were not allowed to pick a single element multiple times, every element will have a couple of options: pick or not pick which is 2^n different recursion calls, also assuming that the average length of every combination generated is k. (to put length k data structure into another data structure)

Why not (2^n) but (2^t) (where n is the size of an array)?

Assume that there is 1 and the target you want to reach is 10 so 10 times you can “pick or not pick” an element.

Space Complexity: O(k*x), k is the average length and x is the no. of combinations

Python Code

from typing import Listclass Solution: def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: ans = [] ds = [] def findCombination(ind: int, target: int): if ind == len(candidates): if target == 0: ans.append(ds[:]) return if candidates[ind] <= target: ds.append(candidates[ind]) findCombination(ind, target - candidates[ind]) ds.pop() findCombination(ind + 1, target) findCombination(0, target) return ansif __name__ == "__main__": obj = Solution() candidates = [2, 3, 6, 7] target = 7 ans = obj.combinationSum(candidates, target) print("Combinations are: ") for i in range(len(ans)): for j in range(len(ans[i])): print(ans[i][j], end=" ") print()


Combinations are:
2 2 3

Time Complexity: O(2^t * k)where t is the target, k is the average length

Reason:Assume if you were not allowed to pick a single element multiple times, every element will have a couple of options: pick or not pick which is 2^n different recursion calls, also assuming that the average length of every combination generated is k. (to put length k data structure into another data structure)

Why not (2^n) but (2^t) (where n is the size of an array)?

Assume that there is 1 and the target you want to reach is 10 so 10 times you can “pick or not pick” an element.

Space Complexity: O(k*x), k is the average length and x is the no. of combinations

Special thanks toRishi Raj GirmalandSudip Ghosh for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam,please check out this article


What is the complexity of combination sum? ›

The time complexity to solve the Combination sum using backtracking is (2^t )* k. Where k is the average length of the input and t is the length of the recursive call.

How do you find the sum of combinations? ›

An easy way to see this is : you will take each element (1), or not (0). So you can represent all binary number with n bits : 2^n. And this amounts to all combination with one item removed, plus all combinations with 2 items removed,and so on.. = sum of C(k/N).

What is the backtracking algorithm for combination sum? ›

Backtracking Algorithm- How It Enables Efficient Combination Sum Computation. The algorithm starts by sorting the input array and then exploring all possible combinations in a depth-first search manner. At each step, the algorithm selects a number from the input array and adds it to the current combination.

What are the ways to represent a number as a sum of 1's and 2's? ›

(1 + 1 + 1 + 1), (2 + 1 + 1), (1 + 2 + 1), (1 + 1 + 2), (2 + 2) answer is 5. And so on. It can be observed that it form Fibonacci Series. So, the number of ways of representing N as a sum of 1s and 2s is (N + 1)th Fibonacci number.

What is the time complexity of combination sum IV? ›

Time Complexity : O(N * T) , where N is the number of elements in nums and T is equal to the given target value. Space Complexity : O(T) . We need O(T) to maintain the dp array and in the worst case, we would have to do T numbers of recursive calls of helper. So, the overall space complexity becomes O(T) .

What is the time complexity of combination sum using recursion? ›

Combination Sum, The number of recursive calls is directly proportional to the input size and the target sum, resulting in an O(2^n) time complexity in the worst case, where n is the input size.

What is the rule of sum combinations? ›

The Sum Rule: If there are n(A) ways to do A and, distinct from them, n(B) ways to do B, then the number of ways to do A or B is n(A) + n(B).

How do you find the sum of all possible combinations in an array? ›

Follow the below steps to implement the idea:
  1. Sort the array arr[] and remove all the duplicates from the arr[] then create a temporary vector r. to store every combination and a vector of vector res.
  2. Recursively follow: If at any time sub-problem sum == 0 then add that array to the res (vector of vectors).
Jan 23, 2023

Are backtracking algorithms greedy? ›

What is backtracking? By being greedy, the algorithm matches the longest possible part. Backtracking algorithms, upon failure, keep exploring other possibilities. Such algorithms begin afresh from where they had originally started, hence they backtrack (go back to the starting point).

What is the rule of sums algorithms? ›

Rule of Sum - Statement:

If there are n n n choices for one action, and m m m choices for another action and the two actions cannot be done at the same time, then there are n + m n+m n+m ways to choose one of these actions.

Does backtracking find all possible solutions? ›

Introduction and Permutation

Backtracking is a general algorithm for finding all (or some) solutions to some computational problems, that incrementally builds candidates to the solutions.

How do you sum numbers between one and n? ›

Sn = n(n+1)/2

Hence, this is the formula to calculate sum of 'n' natural numbers.

What is a number with a sum of 1? ›

The two numbers with the sum of 1 are 0 and 1. What two prime numbers sum up to 99? Very easy question because 99 is an odd number and we know the sum of two odd number is surely an even number, so one of the primes must be odd and the another one must be even.

How many ways can you write 7 as the sum of 1's and 2's? ›

The answer is five. Let's enumerate the different ways.

What is order of 1 in time complexity? ›

In short, O(1) means that it takes a constant time, like 14 nanoseconds, or three minutes no matter the amount of data in the set.

What is backtracking in Ada? ›

Backtracking is an algorithmic technique whose goal is to use brute force to find all solutions to a problem. It entails gradually compiling a set of all possible solutions. Because a problem will have constraints, solutions that do not meet them will be removed.

What time complexity are prefix sums? ›

Its main idea uses prefix sums which are defined as the consecutive totals of the first 0,1,2,...,n elements of an array. We can easily calculate the prefix sums in O(n) time complexity. Notice that the total pk equals pk−1 + ak−1 , so each consecutive value can be calculated in a constant time.

Is recursion bad for time complexity? ›

Recursion can reduce time complexity.

An example of this is calculating fibonacci numbers. If you calculate the fibonacci sequence up to a number n using recursion rather than iteration, the time to complete the task when compared to that of the iterative approach was much greater.

Is recursive faster than iterative? ›

Iteration is faster and more efficient than recursion. It's easier to optimize iterative codes, and they generally have polynomial time complexity.

What is the time complexity of Fibonacci series? ›

The time complexity of the Fibonacci series is T(N), i.e., linear. We have to find the sum of two terms, and it is repeated n times depending on the value of n. The space complexity of the Fibonacci series using dynamic programming is O(1).

How long to try 999 combinations? ›

If you have forgotten your code, the maker of TSA approved locks; Travel Sentry states that you can try every possible combination. This means starting from 000 and working to 999. This method usually takes 10-15 minutes.

How long would it take to try every 4 digit combination? ›

You have 9,999 guesses so if you are not logged out for too many tries and you could key in PINs starting with 0001 going all the way to 9999 at a rate of a PIN every 2 seconds then you are looking at about 5.5 hours to get to 9999.

What is the 3 sum rule? ›

The divisibility rule of 3 states that a whole number is said to be divisible by 3 if the sum of all its digits is exactly divided by 3. Without performing division we can find out whether a number is divisible by 3 or not. For example, 45 is divisible by 3 because the sum of 45 is (4 + 5) = 9, which is divisible by 3.

What is the difference between combination and permutation? ›

In a combination, the elements of the subset can be listed in any order. In a permutation, the elements of the subset are listed in a specific order. All data sets have a finite number of combinations as well as a finite number of permutations. This makes them useful for calculating probability for complex events.

How do you sum all elements in an array without a loop? ›

Sum of array Elements without using loops and recursion
  1. Given an array of N elements, the task is to find the Sum of N elements without using loops(for, while & doWhile) and recursion. ...
  2. Approach: Unconditional Jump Statements can be used to solve this problem. ...
  3. Time complexity: O(N) where N is size of given array.
Mar 13, 2023

What is the maximum sum of a pair of numbers in an array? ›

The maximum pair sum is the largest pair sum in a list of pairs. For example, if we have pairs (1,5) , (2,3) , and (4,4) , the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8 .

What is the perfect sum count? ›

Perfect sums is the sum of two or more number of elements of arrays whose sum is equal to a given number.

Why are greedy algorithms so hard? ›

This is because at each level of recursion the size of gets smaller and the number of sub-problems increases. The difficult part is that for greedy algorithms you have to work much harder to understand correctness issues. Even with the correct algorithm, it is hard to prove why it is correct.

Which problems Cannot be solved by greedy algorithm? ›

0-1 knapsack problem cannot be solved by the greedy method because it is enabled to fill the knapsack to full capacity so here greedy algorithm is not optimal. 0-1 knapsack problem is solved by Dynamic programming approach.

Is Dijkstra a backtracking algorithm? ›

Dijkstra algorithm always finds the shortest path (in graphs without negative edges) and never backtracks.

What is the golden rule of algebra in math? ›

Golden Rule of Algebra: “Do unto one side of the equal sign as you will do to the other…” **Whatever you do on one side of the equal sign, you MUST do the same exact thing on the other side. If you multiply by -2 on the left side, you have to multiply by -2 on the other.

Can algorithms be used to solve every problem? ›

An undecidable problem may have some instances that have an algorithmic solution, but there is no algorithmic solution that could solve all instances of the problem.

Which problem cannot be solved by backtracking? ›

1. Which of the problems cannot be solved by backtracking method? Explanation: N-queen problem, subset sum problem, Hamiltonian circuit problems can be solved by backtracking method whereas travelling salesman problem is solved by Branch and bound method.

Why is backtracking difficult? ›

The hardest part of a backtracking problem is determining how the state of the problem changes based on the choice made, and what to do if a recursive call fails or succeeds, which also influences the base case.

Is backtracking better than brute force? ›

Overview. Backtracking is an improvement of the bruteforce approach. It tries to search for a solution to a problem among all the available options. It finds a solution set by developing a solution step by step, increasing levels with time, using recursive calling.

What is the complexity of sum function? ›

The time complexity of the sum function in Python is O(n) in the average case scenario, where "n" is the number of elements in the iterable object. This means that the time taken to calculate the sum increases linearly with the number of elements in the iterable.

What is complexity in C Plus Plus? ›

Time Complexity of Algorithms in C++ The time complexity of algorithms means the time it takes for an algorithm to run as being a function of the same length as the input. In this article, I will introduce you to the concept of time complexity of algorithms and its examples by using the C ++ programming language.

What is the complexity of two sum solution? ›

Complexity Analysis
  • Time complexity: O(n2). For each element, we try to find its complement by looping through the rest of the array which takes O(n) time. Therefore, the time complexity is O(n2).
  • Space complexity: O(1). The space required does not depend on the size of the input array, so only constant space is used.

What is the complexity of binary sum? ›

the running time of binarySum is O(n), as there are 2n−1 method calls, each requiring constant time.

What is the runtime complexity of 3 sum? ›

Three Sums: Sorted

Second, the loop in two sum sorted is O(N) in complexity, which we applied N times in three sum sorted algorithm, so the complexity of it is O(N²).

What is the disadvantage of sum function? ›

The Limitations of the SUM Function in Excel

The drawbacks of the function are listed as follows: The cell range supplied must match the dimensions of the source. The cell containing the output must always be formatted as a number.

What is the time complexity of sum of first n numbers? ›

Time Complexity: O(N), for traversing from 1 till N to calculate the required sum. Auxiliary Space: O(1), as constant extra space is required.

What are the 4 levels of complexity? ›

According to project management experts Remington and Pollack, there are four types of complexity that determine the selection of projects. These include structural, technical, temporal, and directional complexity.

What are the 3 levels of complexity? ›

  • 3 levels of technical complexity. In the movie Inception, there are deeper levels of dreams, entered via free-fall. ...
  • Level 1 - Help the audience understand real life impacts. ...
  • Level 2 - Bridge the context to abstract or technical. ...
  • Level 3 - Technical deep dives exist to do something.
Dec 6, 2020

What is complexity in easy words? ›

Complexity is the state of having many different parts connected or related to each other in a complicated way. ... a diplomatic tangle of great complexity.

What is the time complexity of sum of n digits? ›

The time complexity to find the sum of digits of a number in C is O(log(n)) where n is the number.

What algorithm is two sum? ›

The two-sum problem is a question that asks that if given an array of integers (numbers), like [1, 2, 3], and a target sum number, such as 5, return an array of elements that add up to that target sum number. If no two numbers in the array add up to the target number, then we need to return an empty array; [].

How do you solve a two sum algorithm? ›

We can efficiently solve this problem using sets. We can add every element of arr if the target - arr[i] value exists in the sums set. This implies that target = arr[i] + (the value in sums) , which this will be a solution. This algorithm will run in O ( n ) O(n) O(n).

How to do binary sum in C? ›

  1. Take two binary numbers as input and store it in the variables binary1 and binary2.
  2. Initialize the variables i and remainder to zero.
  3. Obtain the remainders of both the binary numbers.
  4. Obtain the quotients of both the binary numbers.

What is the time complexity of prefix sum algorithm? ›

The algorithm will loop n times, and since the last iteration will require the most amout of computations (n computations) the algorithm will in total make n^2 + f(n) computations. where f(n) is a polynomial of degree n^2 or less. Therefore this algorithm is quadratic O(n^2) .

What is the time complexity of Bitwise and Python? ›

Usually we consider bitwise operations to have O(1) worst-case time complexity because of built-in hardware support in most platforms.


Top Articles
Latest Posts
Article information

Author: Dan Stracke

Last Updated: 08/13/2023

Views: 5511

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.