AfterAcademy Tech
•
18 Jul 2020

Difficulty: Medium
Asked in: Amazon, Facebook, Adobe
Problem Description
Given an array of integers arr[] and a target number k, write a program to find all unique combinations in arr[] such that the sum of all integers in the combination is equal to k.
Problem Note:
k) will be positive integers.arr[] the unlimited number of times.a1, a2, … , ak) must be in non-descending order. (i.e, a1 ≤ a2 ≤ … ≤ ak).Example 1
Input: arr[] = [2, 3, 6, 7], k = 7
Output:
[
[7],
[2,2,3]
]
Explanation: All the unique combinations whose sum is 7 is printed.
2+2+3 = 7
7 = 7
Example 2
Input: arr[] = [2,3,5], k = 8
Output:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
Explanation: All the unique combinations whose sum is 8 is printed.
2+2+2+2 = 8
2+3+3 = 8
3+5 = 8
First of all, we need to sort the array, to ensure the combination to be unique and also the numbers in the combination to be listed in a non-descending order.
Then, we define a recursive function with the signature as:
backtrack(int[][] res, int[] candidate, int [] nums, int target, int start)
where "candidate" is the list that we could take numbers from, "result" is the list of combination that we have accumulated so far, "start" is the starting point at which we could take numbers from forwards (no backward), and "target"is the rest sum that we need to achieve. A recursion tree for the above example might look like this →

As a recursive function, the bottom cases for the backracking() are:
start is equal to the target, we then find a combination, so just add the start to the result and output the result.start is greater than the target, then it is impossible to find the combination onwards because the candidates the list is sorted and all the following elements would be greater than the target as well.Given the above recursive function, we could solve the problem by calling the function for each candidate starting from the start point.
For each candidate, we first try to add the candidate into the result, and then again starting from this candidate, we call the backtracking() function to obtain the result for the new target afterwards.
Solution Steps
result array to store the valid sequencescurr array that will store the current sequence found in the path of the recursion tree.target becomes less than 0.target becomes 0 then add the candidate array to the result as the values in the candidate array must be sum up to the given target.Pseudo Code
int[][] combinationSum(int[] nums, int target) {
int[][] result
sort(nums)
int[] curr
backtrack(result, curr, nums, target, 0)
return result
}
void backtrack(int[][] res, int[] candidate, int [] nums, int target, int start) {
if(target < 0)
return
else
if(target == 0)
res.add(candidate)
else{
for(int i = start; i < nums.length; i++) {
candidate.add(nums[i])
backtrack(list, candidate, nums, target - nums[i], i)
// not i + 1 because we can reuse same elements
candidate.remove(candidate.size() - 1)
}
}
}
Complexity Analysis
Time Complexity: O(l^k)
target / minInArray).Space Complexity: O(k), for storing the path, which could be k long at most.
Critical Ideas To Think
backtrack function why did we start the for loop from start than start + 1 ?Happy Coding!
Team AfterAcademy!!
AfterAcademy Tech
Given two integers n and k, Write a program to return all possible combinations of k numbers out of 1 2 3 ... n. Elements in a combination must be in a non-descending order. The combinations themselves must be sorted in ascending order, i.e., the combination with the smallest first element should be printed first.

AfterAcademy Tech
Given a binary tree and a sum, write a program to determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Its an easy problem based on Binary tree traversal and a famous interview question

AfterAcademy Tech
You are given an array A[] with n elements. You need to find the maximum sum of a subarray among all subarrays of that array. A subarray of array A[] of length n is a contiguous segment from A[i] through A[j] where 0<= i <= j <= n.

AfterAcademy Tech
Given an array arr[ ] of n integers, are there elements x, y, z in arr such that x + y + z = 0? Find all unique triplets in the array which gives the sum of zero. The problem is asked in many interviews and required do be solved in O(n^2) with constant space.
