13
2
The \$d\$-dimensional vector space \$\mathbb{F}_2^d\$ over the field with two elements \$\mathbb{F}_2\$ is the set of vectors of \$d\$ bits. Addition of vectors is bitwise xor. A linear dependence is a set of vectors whose total (xor them all together) is \$0\$. Every set of \$d+1\$ vectors in \$\mathbb{F}_2^d\$ contains a linear dependence. We will represent the elements of \$\mathbb{F}_2^d\$ as the integers \$0\$ through \$2^{d}-1\$ using the binary expansion.
Task
Input: a positive integer \$d\$ and a list of \$d+1\$ positive integers between \$1\$ and \$2^{d}-1\$. You may assume that the list is sorted and you may assume that the entries are distinct.
Output: A nonempty subset of the \$d+1\$ numbers whose bitwise xor is zero.
Scoring:
This is code golf so shortest answer in bytes wins.
Examples:
Input: (3, [1,3,4,7])
Output: [3, 4, 7]
In binary, these are 001, 011, 100, and 111. We see that 100 xor 011 xor 111 = 000. In this case this is the only solution, so we must output [3, 4, 7].
Test cases:
Input: (3, [1,3,4,7])
Output: [3, 4, 7]
Input: (4, [1, 2, 6, 10, 30])
Output: Behavior undetermined because 30 > 15 = 2^4 - 1.
Input: (5, [3, 5, 6, 9, 14])
Output: [3, 5, 6]
Input: (5, [4, 8, 10, 12, 14])
Output: [4, 8, 12] or [4, 10, 14], or [8, 10, 12, 14].
Input: (6, [4, 15, 17, 21, 49, 51, 57])
Output: [4, 17, 21]
Input: (6, [1, 7, 8, 15, 39, 55, 57])
Output: [7, 8, 15] or [1, 15, 55, 57] or [1, 7, 8, 55, 57]
Input: (6, [13, 14, 24, 33, 35, 36, 63])
Output: [13, 14, 24, 36, 63]
Input: (6, [10, 11, 25, 28, 40, 59, 62])
Output: [10, 25, 40, 59] or [10, 28, 40, 62], or [25, 28, 59, 62]
Input: (6, [1, 3, 5, 11, 19, 32, 63])
Output: [1, 3, 5, 11, 19, 32, 63]
2May we return all solutions? – Arnauld – 2019-12-20T19:48:32.403
5You may want to clarify that the empty set is not an acceptable solution. – Arnauld – 2019-12-20T19:50:32.673
1Must the answer be a list of the entries, can it be a bitmask with the ith bit set iff item[i] is in the set? – harold – 2019-12-20T21:10:05.087
@harold Yes, the bitmask sounds fine to me. – Hood – 2019-12-20T22:22:15.137
@Arnauld I think returning all solutions should be fine. I agree it would be good to clarify on the empty set, I meant to do that when writing the question but forgot. – Hood – 2019-12-20T22:22:59.213
1May we assume the entries are distinct? – xnor – 2019-12-20T23:34:33.913
@xnor Yes, will add that. – Hood – 2019-12-20T23:44:37.157
What is the maximum value for $d$? – qwr – 2019-12-24T01:02:46.283
@qwr I guess you are asking because harold's answer only handles $d\leq 32$? I think that's alright, certainly it's larger than any of my test cases. Many of the other solutions use brute force and would never finish if $d=32$. Plus it's nice for people to have fun and it doesn't seem like all that many people golf assembly language. – Hood – 2019-12-24T05:34:54.640