14
Working on something in probability theory, I stumbled across another combinatorical exercise. These are always fun to solve, searching for intelligent approaches. Of course, one can use brute force for the following task, but this challenge is restricted complexity, so this is not allowed.
Task
Given an integer Input \$n > 1\$, print all integer vectors of length \$n\$, respecting following conditions:
- \$v_1 = 1\$
- \$v_{i+1} - v_i \in \{0,1\}\$
- \$v_n \neq n\$
example:
Input:
n=4
Output:
[1 1 1 1]
[1 1 1 2]
[1 1 2 2]
[1 1 2 3]
[1 2 2 2]
[1 2 2 3]
[1 2 3 3]
Additionally, time complexity of your solution must be \$O(2^n)\$. Note that generating all vectors and then filtering would be \$O(n^n)\$, which is not acceptable. A direct construction, not generating any false vector or duplicate vector at all, seems to be a bit more of a challenge.
Having said that, shortest code in bytes, respecting the above condition, wins.
3Would it be correct to assume [1 2 3 4] would also be a valid output for your example, meaning there would be 2^(n-1) total outputs? – Mathgeek – 2020-02-11T14:11:48.303
@Mathgeek It says containing integers < n, so I assumed you have to exclude that vector. – Neil – 2020-02-11T14:25:38.040
Indeed, I would edit the question and shift that part to the conditions list, but it seems i'm not allowed to. – Kenji – 2020-02-11T14:28:07.617
So it just means we have to pop the [1, 2, 3, 4... n] vector. Extra restriction never hurt anybody! – Mathgeek – 2020-02-11T14:28:50.030
1@Neil so this is essentially what mathgeek was saying, except we have $2^{n-1}-1$ – RGS – 2020-02-11T14:29:48.840
5brute force (…) wouldn't fulfill winning criteria. contradicts [tag:code-golf]. – Adám – 2020-02-11T14:33:28.440
1@Adám Is there a standard way to ban brute force answers? [tag:restricted-complexity] ? – Anush – 2020-02-11T14:38:27.927
@Anush Say: solutions must terminate within X Ys on Z and supply a test case that can't be brute forced within those constraints. – Adám – 2020-02-11T14:40:25.377
@Adám But then you have to test all the code on your own PC? – Anush – 2020-02-11T14:42:16.890
@Anush Usually people will link to TIO or similar, and it'll fail on big cases. – Adám – 2020-02-11T14:42:51.767
@Adám OK I am happy with that. People also complain that the timing on TIO is unreliable and so shouldn't be used for anything related to time constraints. But I don't agree. – Anush – 2020-02-11T14:44:07.393
@Anush While it is very imprecise, it won't usually fluctuate by a factor of 10… – Adám – 2020-02-11T14:44:57.393
2Welcome to the site. I am voting to close this as unclear. This is because a blanket ban on "brute force" answers is not objective. There is a ton of grey area as to what counts as "direct construction". You can make this [tag:restricted-complexity] to make it objective, where you specify maximum asymptotic complexity requirements, a measure that is objective and indisputable. – Post Rock Garf Hunter – 2020-02-11T14:55:26.643
I aggree that the brute force part may provoke problems. I tried to heal this by specifing that no supersets should be used, apart from that, anything is allowed. If that doesn't suffice, we may remove the brute force ban completely – Kenji – 2020-02-11T15:03:20.210
1I'm a bit lost. So, can brute force be used or not? Is there any restriction on the approaches that can be used? (note that this can be hard to enforce or verify) – Luis Mendo – 2020-02-11T17:22:30.257
2
I'm voting to close this question as off-topic because it has an unobservable requirement making it impossible to objectively tell if any given answer is valid and thus it's not clear if it wins.
– Giuseppe – 2020-02-11T17:52:02.870Sad that this good challenge got closed over a minor detail. I rephrased the requirement to be explicitly restricted-complexity and voted to reopen. – Grimmy – 2020-02-12T15:53:40.700