arrays,algorithm,divide-and-conquer,subset-sum,sub-arrays

here is brute-force java solution you can use, note - complexity of this solution is O(3^N), which is very very slow /** * Returns absolute difference between 3 values in array */ static int getdiff(final int s[]) { return Math.abs(s[0] - s[1]) + Math.abs(s[1] - s[2]) + Math.abs(s[2] - s[0]);...

algorithm,np-complete,subset-sum

I would recommend doing this reduction in two steps. First, reduce subset-sum to the partition problem. This problem is related to subset-sum, except that instead of being given a target k, the goal is to split the set into two exactly equal halves. This isn't too hard (I'll leave it...

r,data.table,plyr,dplyr,subset-sum

You may try aggregate aggregate(d ~ a + b + c, data = df, sum) # a b c d # 1 1 1 1 500 # 2 1 3 1 0 # 3 1 1 2 600 # 4 1 2 3 300 As noted by @Roland, for bigger...

Based on your solution: def subsetsum(array,num): if num == 0 or num < 1: return None elif len(array) == 0: return None else: if array[0] == num: return [array[0]] else: with_v = subsetsum(array[1:],(num - array[0])) if with_v: return [array[0]] + with_v else: return subsetsum(array[1:],num) ...

dynamic-programming,recurrence-relation,subset-sum

You are almost correct ( not sure why you used min ). But let dp[i][j] store the answer of whether a subset of arr[0],arr[1],....arr[j] (here arr[] is the array of elements ) can sum upto i. That is dp[i][j] is 1 if answer is yes and 0 if answer is...

c#,algorithm,.net-2.0,dynamic-programming,subset-sum

My previous answer works on the principle of cutting off the number of combinations to check. But this can be improved significantly once you sort the array. The principle is similar, but since the solution is entirely different, I've decided to put it in a separate answer. I was careful...

java,algorithm,recursion,big-o,subset-sum

Some minor optimizations if I understand this correctly: public static void recDfs(int[] arr, int k, int sum) { if (sum < 0) return; if (sum == 0) { counter++; return; } if (k == 0) { return; } recDfs(arr, k - 1, sum - arr[k - 1]); recDfs(arr, k -...

c,algorithm,recursion,subset-sum

#include <stdio.h> #define SIZE(a) sizeof(a)/sizeof(a[0]) int binary[100]; void show(int* p, int size) { int j; for (j = 0; j < size; j++) printf("%d\n", p[j]); } void foo(int target, int i, int sum, int *a, int size) { if (sum == target) { // solution found show(binary, size); } else...

algorithm,scala,subset,subset-sum

Making a full list of sums is unnecessary, and using BigInt is wasteful regardless--a Long can hold up to 9.2e18, and you are promised that only 1e15 is going to appear. (In fact, I think 1e15 was chosen so that even a Double can hold the answer without loss of...

java,arrays,algorithm,subset,subset-sum

Your solution is wrong because it's a greedy approach. It decides if you should add a number or not based on the fact that adding it does not violate the sum, at the moment. However, this greedy approach does not work, with a simple example of the following array: [1,9,6,5]...

I think your idea is mostly right: generate each combination of the terms, do the sum and see if it's a hit. You can optimize your code though. The problem is that once you generate 1 + 2, you see that it's not a match for your desired sum and...

algorithm,dynamic-programming,memoization,subset-sum

Suggestion: The way you are solving will not work because of complexity. Although space complexity will work (limit is 1536MB and space used is around 785MB), but time complexity is too high for time limit of 5s. An estimate is 10^8 could be executed safely within 1 sec. If you...