Facebook Interview Question
Research ScientistsCountry: United States
Interview Type: Phone Interview
Recursive solution, complexity O(2^(countofitems<targetsum))
package main
import (
"fmt"
"sort"
)
func main() {
input := []int{1, 1, 3, 4, 1, 1, 2, 3}
target := 4
// sorting can be done O(n)
sort.Ints(input)
used := make([]bool, len(input))
findSum(input, used, target, 0)
}
func print(used []bool, input []int) {
for i := 0; i < len(input)-1; i++ {
if used[i] {
fmt.Printf("%d ", input[i])
}
}
fmt.Println()
}
func findSum(input []int, used []bool, target, cur int) {
if cur > target {
return
}
if cur == target {
print(used, input)
return
}
for i := 0; i < len(input)-1; i++ {
if input[i] > target {
return
}
if used[i] {
continue
}
used[i] = true
findSum(input, used, target, cur+input[i])
used[i] = false
}
}
Cool to see @ChrisK back. Welcome back dude. However, as a welcome gesture,
I will love to rub something in... and here we go:
/* This is how to ZoomBA */
a = [1, 1, 3, 4, 1, 1, 2, 3]
target = 4
range_a = [0:size(a)].asList
// sequences generates all possible subset of a set
options = from ( sequences( range_a ), set() ) as {
// map back the opt to actual
list( $.o ) -> { a[$.o] }
// the where clause filter around it
} where { sum($.o) == target }
// prints it
println(options)
class subset_sum {
static ArrayList<ArrayList<Integer>> subsetSum(int set[], int n, int sum) {
ArrayList<ArrayList<Integer>> subset[][] = new ArrayList[sum+1][n+1];
for (int i = 0; i <= n; i++) {
subset[0][i] = new ArrayList<>();
subset[0][i].add(new ArrayList<Integer>());
}
for (int i = 1; i <= sum; i++) {
subset[i][0] = null;
}
// Fill the subset table in bottom up manner
for (int i = 1; i <= sum; i++) {
for (int j = 1; j <= n; j++) {
subset[i][j] = subset[i][j-1];
if (i >= set[j-1] && subset[i - set[j-1]][j-1] != null) {
if (subset[i][j] == null) {
subset[i][j] = new ArrayList<ArrayList<Integer>>();
} else {
subset[i][j] = new ArrayList<ArrayList<Integer>>(subset[i][j-1]);
}
for (ArrayList<Integer> list: subset[i - set[j-1]][j-1]) {
ArrayList<Integer> newList = new ArrayList<Integer>(list);
newList.add(set[j-1]);
subset[i][j].add(newList);
}
}
}
}
return subset[sum][n];
}
class subset_sum {
static ArrayList<ArrayList<Integer>> subsetSum(int set[], int n, int sum) {
ArrayList<ArrayList<Integer>> subset[][] = new ArrayList[sum+1][n+1];
for (int i = 0; i <= n; i++) {
subset[0][i] = new ArrayList<>();
subset[0][i].add(new ArrayList<Integer>());
}
for (int i = 1; i <= sum; i++) {
subset[i][0] = null;
}
// Fill the subset table in bottom up manner
for (int i = 1; i <= sum; i++) {
for (int j = 1; j <= n; j++) {
subset[i][j] = subset[i][j-1];
if (i >= set[j-1] && subset[i - set[j-1]][j-1] != null) {
if (subset[i][j] == null) {
subset[i][j] = new ArrayList<ArrayList<Integer>>();
} else {
subset[i][j] = new ArrayList<ArrayList<Integer>>(subset[i][j-1]);
}
for (ArrayList<Integer> list: subset[i - set[j-1]][j-1]) {
ArrayList<Integer> newList = new ArrayList<Integer>(list);
newList.add(set[j-1]);
subset[i][j].add(newList);
}
}
}
}
return subset[sum][n];
}
vector<vector<int>> SumSets(vector<int> const &a, int sum,
unordered_map<pair<int, int>, vector<vector<int>>> &memo, int idx = 0)
{
vector<vector<int>> sets;
if (sum == 0) {
sets.push_back(vector<int>());
return sets;
}
if (sum < 0) {
return sets;
}
pair<int, int> memo_key = pair<int, int>(idx, sum);
auto it = memo.find(memo_key);
if (it != memo.end()) {
return it->second;
}
for (int i = idx; i < a.size(); ++i) {
vector<vector<int>> subsets = SumSets(a, sum - a[i], memo, i + 1);
for (auto const & subset : subsets) {
vector<int> set{a[i]};
set.insert(set.end(), subset.begin(), subset.end());
sets.push_back(set);
}
}
memo[memo_key] = sets;
return memo[memo_key];
}
- Chris April 27, 2017