10
Remember that a set is unordered without duplicates.
Definition An N-uniquely additive set S whose length is K is a set such that all N-length subsets in S sum to different numbers. In other words, the sums of all the N-length subsets of S are all distinct.
Objective Given an array/set as input and a number N
to a function or to a full program in any reasonable format, find and return or output a truthy or falsey value (erroring for falsey is okay) denoting whether or not the input is N-uniquely additive.
You may assume that each element only appears at most once and that each number is within your language's native datatype. If need be, you may also assume that the input is sorted. Last, you may assume that 0 < N <= K
.
Examples
Let's consider the set S = {1, 2, 3, 5}
and N = 2
. Here are all the sums of all unique pairs over S
(for the unique ones are the only ones of interest for sums):
1 + 2 = 3
1 + 3 = 4
1 + 5 = 6
2 + 3 = 5
2 + 5 = 7
3 + 5 = 8
We can see that there are no duplicates in output, so S is 2-uniquely additive.
Let's now consider the set T = {12, 17, 44, 80, 82, 90}
and N = 4
. Here are all the possible sums of length four:
12 + 17 + 44 + 80 = 153
12 + 17 + 44 + 82 = 155
12 + 17 + 44 + 90 = 163
12 + 17 + 80 + 82 = 191
12 + 17 + 80 + 90 = 199
12 + 17 + 82 + 90 = 201
12 + 44 + 80 + 82 = 218
12 + 44 + 80 + 90 = 226
12 + 44 + 82 + 90 = 228
12 + 80 + 82 + 90 = 264
17 + 44 + 80 + 82 = 223
17 + 44 + 80 + 90 = 231
17 + 44 + 82 + 90 = 233
17 + 80 + 82 + 90 = 269
44 + 80 + 82 + 90 = 296
They are all unique, and so T is 4-uniquely additive.
Test Cases
[members], N => output
[1, 4, 8], 1 => true
[1, 10, 42], 1 => true ; all sets trivially satisfy N = 1
[1, 2, 3, 4], 3 => true
[1, 2, 3, 4, 5], 5 => true
[1, 2, 3, 5, 8], 3 => true
[1, 2, 3, 4, 5], 2 => false ; 1 + 4 = 5 = 2 + 3
[-2, -1, 0, 1, 2], 3 => false ; -2 + -1 + 2 = -1 = -2 + 0 + 1
[1, 2, 3, 5, 8, 13], 3 => false ; 1 + 2 + 13 = 16 = 3 + 5 + 8
[1, 2, 4, 8, 16, 32], 3 => true
[1, 2, 4, 8, 16, 32], 4 => true
[1, 2, 4, 8, 16, 32], 5 => true
[1, 2, 4, 8, 16, 32], 6 => true
[3, 4, 7, 9, 12, 16, 18], 6 => true
[3, 4, 7, 9, 12, 16, 18], 3 => false ; 3 + 4 + 12 = 19 = 3 + 7 + 9
You mean
N <= K
? – Neil – 2016-06-03T22:34:49.547@Neil Yes, I do. Sorry! – Conor O'Brien – 2016-06-03T22:35:07.210
Does an error count as something
falsey
? – flawr – 2016-06-03T22:49:29.093@flawr Sure, I'll accept that – Conor O'Brien – 2016-06-03T22:50:07.510