5
Problem Description
Given:
- a function \$f(a, b, c, d, e) = \frac{a \times b}c + \frac de\$
- an array \$x = [x_1, x_2, x_3, x_4, ..., x_n]\$ of non-distinct integers
- a target value \$k\$
What is the most efficient way, in terms of worst-case \$n\$, to find 5 distinct indices \$x_a, x_b, x_c, x_d, x_e\$ from \$x\$ such that \$f(x[x_a], x[x_b], x[x_c], x[x_d], x[x_e]) = k\$?
Example solutions
Example 1 (single solution):
\$x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], k = 5\$
Solution 1:
\$[1, 4, 2, 9, 3]\$ as \$f(x[1], x[4], x[2], x[9], x[3])\$ evaluates to \$5\$.
Example 2 (multiple solutions):
\$x = [0, -1, 1, -1, 0, -1, 0, 1], k = 0\$
Solution 2 (of many solutions):
\$[1, 2, 3, 5, 7]\$ OR \$[0, 1, 2, 4, 7]\$
Example 3 (no solution):
\$x = [0, -1, 1, -1, 0, -1, 0, 1], k = 8\$
Solution 3:
\$-1\$ (no solution)
Example 4 (intermediary floats):
\$x = [2, 3, 5, 4, 5, 1], k = 2\$
Solution 4:
\$[2, 3, 5, 4, 5]\$
Are you given a function with some number of variables an array and a third value? Also, I think the most efficient way is just brute force permutations from the array. – fəˈnɛtɪk – 2018-06-12T22:09:30.610
@fəˈnɛtɪk brute force with early stopping for branches of values larger than
k
? Also, the interchangeability ofa*b == b*a
seems important? – Seanny123 – 2018-06-12T22:14:42.000@fəˈnɛtɪk The function is fixed. I think there might be some way to optimize this solution like two sum, but I haven't been able to figure out how yet.
– versatile parsley – 2018-06-12T22:15:53.7031What exactly does the array contain? Floats? Integers? Positive integers? – Arnauld – 2018-06-12T23:36:47.327
May the intermediate calculations lead to float results? For instance, is
[2,3,5,4,5]
a valid solution fork=2
? – Arnauld – 2018-06-13T11:26:11.030@user202729 in terms of n – versatile parsley – 2018-06-13T21:53:54.913
@Arnauld yes, that's valid – versatile parsley – 2018-06-13T21:54:17.807
@user202729 I have added all the edits and clarifications requested in the discussion. Is this eligible to be reopened? – versatile parsley – 2018-06-20T01:47:13.487
@fəˈnɛtɪk That will still be O(n^5) in worst case
n
. – user202729 – 2018-06-21T02:30:11.1432Do we know if k is always an integer? it is not specified in the question. – fəˈnɛtɪk – 2018-06-21T16:17:33.963
I guess that "non-distinct" means "not necessarily distinct"? It seems to be possible in O(n^4), maybe O(n^3 log n). – Christian Sievers – 2018-06-22T18:08:02.000
The first example also has the solution
[0,2,3,5,1]
. The last example gives the values instead of the indices. – Christian Sievers – 2018-06-23T13:52:20.440