Summary/outline
This is my second proposal. My initial answer was proven false, and I plan on removing it by next week. Because this is quite different, I decided to post a new answer instead of editing my previous one. If that is contrary to the network policy, I apologize in advance.
This is rather lengthy, so a tl;dr: I claim there's a way to solve the problem with a $k\log k$ test. [EDIT]: I now believe this can be done in $\mathcal O(k)$. Originally, the $k\log k$ cost came from a sort operation that can in fact be simplified. More details in the new section dedicated to the complexity, after I explain the principle of the method.
I introduce a characterization of the desired subset, and then explain the principle
of the algorithm I derive from that characterization. Because that algorithm fails if we use its naive introduction, I then explain how to deal with the issue.
Finally I provide some pseudo-code.
Necessary property
Claim: There exists a good subset $M$ with $\lvert M\rvert\ge 3$ if and only if
there are two indices $i$ and $j$ such that:
- $i<j<k$
- $a_i+a_j <A$, and
- $a_i+a_j+\sum_{l=j+1}^ka_l\ge A$
Proof: We proceed by double implication.
Let $M$ a good subset with cardinality at least 3. Let $i<j$ be its two minimum indices. By condition (ii), $a_i+a_j< A$.
By condition (i) we have
$a_i+a_j+\sum_{l=j+1}^ka_l \ge \sum_{l\in M}a_l \ge A$.
Therefore the two minimum indices from $M$ satisfy the property.
Conversely, suppose there are two indices $i$ and $j$ that satisfy the property. Initialize $M=\{i,j\}$.
Loop over index $l$, starting from the value $j+1$ up to $k$. Add index $l$ to $M$ if the sum over elements in $M$ is strictly smaller than $A$. If the sum is greater than or equal to $A$, we can stop.
Because we add elements that are smaller and smaller, the resulting $M$ will be good. In other words when we finally reach a subset $M$ whose sum is greater than (or equal to) $A$, condition (ii) will also be fulfilled.
Principle of the method
This equivalence suggests that we can loop over possible indices $j$, that is $2\le j\le k-1$, and try to look for some index $i$ that fits the criterion.
That is, given some index $j$, can we find an index $i$ such that:
- $i<j$
- $A-\sum_{l=j}^ka_l\le a_i< A-a_j$
Note that $i<j$ implies $a_i\ge a_j$, thus $a_i$ must also fulfill that inequality.
Ideally, I would like to define
$b^-_j=\max\{ a_j;\ A-\sum_{l=j}^ka_l\}$ and $b^+_j=A-a_j$, then say that
it suffices to find some index $i$ such that
$b^-_j\le a_i<b^+_j$.
That however does not work because $a_i\ge a_j$ does not imply $i<j$.
Indeed,
some of the values $a_{i/j}$ can be repeated.
[EDIT]: As someone pointed out in the comments, this problem can also be expressed as finding
$i$ with $b^-_j\le a_i<b^+_j$, but then you realize that $i=j$.
IF we actually had equivalence, we could achieve a test for a good
subset $M$, $\lvert M\rvert\ge 3$ by using a sorted list.
First we drop any interval $[b^-_j,b^+_j)$ that is empty, in other words if
$b^+_j\le b^-_j$, we can forget about that particular $j$ candidate.
Then we sort every (remaining) $b^-_j$, $b^+_j$ and $a_i$,
with the convention that for equal values,
$b^-_j$ is smaller than $a_i$,
and $a_i$ is smaller than $b^+_j$.
We could go over the sorted list and:
- When we pop out some $b^-_j$, the corresponding interval is declared "open".
- When we pop out some $b^+_j$, the corresponding interval is declared "closed".
- When we pop out some $a_i$, two cases arise. If some interval is "open", then we have just found a good pair $i,j$. If no interval is "open", then we just continue with other values.
On the complexity
[EDIT]: I changed my complexity claim to linear (previously $k\log k$).
In general, sorting arbitrary lists is a $k\log k$ process, so the procedure above would have $k\log k$ complexity. However, here the lists are not arbitrary.
We actually have to merge lists that are already sorted, and that can be done in linear time.
Indeed, the $a_i$ are decreasing, and thus $b^+_j=A-a_j$ is increasing.
Because the $a_i$'s are positive integers, the sum $\sum_{l=j}^ka_l$ is strictly decreasing, and thus $A-\sum_{l=j}^ka_l$ is strictly increasing.
It follows that $b^-_j$ is a maximum between two monotonous sequences and can also easily be merged.
These remarks also hold with the modified values $\hat a_i$, $\hat b_j^-$ and
$\hat b^+_j$ that I introduce below.
Solving the issue of repeated values ($i\ge j$, $a_i\le a_j$).
For my proposed method to work, we need to obtain a way to have an equivalence between $i<j$ and $a_i>a_j$.
Because we are working with integers, this can be achieved by defining new values $\hat a_i$ that are not necessarily integers.
If $a_i$ appears only once, we let $\hat a_i=a_i$.
If the value $a_i$ appears several times, say $n$ times at indices
$i_1<\ldots<i_m<\ldots<i_n$. Then we let $\hat a_{i_m}=a_i+\frac{m-1}n$.
We thus obtain non-integer values with
$$a_i+1 > \hat a_{i_1} >\ldots> \hat a_{i_m} >\ldots> \hat a_{i_n}=a_i$$
It follows that $i<j$ is equivalent to $\hat a_i>\hat a_j$.
[EDIT]: Tl;dr small change to the definition of $\hat b^-_j$ so that my claim 2 can have a correct proof. That change requires a new notation $n_i$.
For an index $i$, let $n_i$ be the number of times the value $a_i$ appears in the original list (possibly $n_i=1$). Notice that $\frac 1{n_i}$ is the distance between consecutive values $\hat a_{i_m}$ and $\hat a_{i_{m+1}}$, when $n_i\ge 2$.
Then we define
$\hat b^-_j=\max\{ \hat a_j+\frac 1{2n_j};\ A-\sum_{l=j}^ka_l\}$ and
$\hat b^+_j=A-a_j$.
Observe that regardless of the value of $n_j$, we have
$$\hat a_{j-1} > \hat a_j+\frac 1{2n_j} > \hat a_{j} $$
[EDIT]: Claim 2 remains unchanged, but its proof has changed to reflect the change in definition.
Claim 2:
We have $\hat b^-_j\le \hat a_i< \hat b_j^+$ if and only if
- $i<j$, and
- $A-\sum_{l=j}^ka_l\le a_i< A-a_j$
Proof: Assume first that $\hat b^-_j\le \hat a_i< \hat b^+_j$.
By definition of the various terms we have
$$
\hat a_i\ge\hat b^-_j\ge\hat a_j+\frac 1{2n_j}>\hat a_j
$$
The part that actually interests us is $\hat a_i>\hat a_j$, since it implies $i<j$. We get our first condition.
For the second condition, notice that because
$A-\sum_{l=j}^ka_l$ is an integer we can use the floor function and preserve inequalities:
\begin{align*}
\hat b^-_j\le \hat a_i< \hat b^+_j
&\implies A-\sum_{l=j}^ka_l\le \hat a_i< A-a_j\\
&\implies \left\lfloor A-\sum_{l=j}^ka_l\right\rfloor\le \lfloor\hat a_i\rfloor< A-a_j\\
&\implies A-\sum_{l=j}^ka_l\le a_i< A-a_j
\end{align*}
Thus, the two conditions are verified.
Conversely, suppose that $i<j$ and
$A-\sum_{l=j}^ka_l\le a_i< A-a_j$.
Because
$$A-\sum_{l=j}^ka_l\le a_i\le \hat a_i< a_i+1\le A-a_j=\hat b^+_j$$
we have
$$A-\sum_{l=j}^ka_l\le \hat a_i<\hat b^+_j$$
Because $i<j$ we know that
$$
\hat a_i\ge \hat a_{j-1}>\hat a_j +\frac 1{2n_j}
$$
Combined with the above, we deduce
$\hat b^-_j\le \hat a_i$. The two conditions thus imply
$\hat b^-_j\le \hat a_i<\hat b^+_j$, which concludes the proof.
Pseudo-code
In this pseudo-code, b+_j
denotes $\hat b^+_j$, likewise b-_j
is for $\hat b^-_j$. Also, a_i
actually denotes $\hat a_i$.
Computing the values of $\hat a_i$ can be done in linear time wrt $k$.
Compute the various b+_j and b-_j //linear wrt k
Discard any pair b-_j/b+_j if b+_j <= b-j //forget about intervals with no solution
Sort the b+_j, b-_j and a_i in a list L //k log k
state = [0,...,0] //array of k values for interval status: 0=closed, 1=open
open_flag = 0 //counter value indicating if some interval is open
for x in L:
if x is an element of type b-_j: //open interval j
state[j] = 1
open_flag += 1
end if
if x is an element of type b+_j: //close interval j
state[j] = 0
open_flag -= 1
end if
if x is an element of type a_i: //check if an interval is open
if open_flag > 0:
return True or find a good pair i,j using the "state" array
end if
end if
end for //this loop was linear wrt the size of L, thus linear wrt k
return False //if we reach this point, we never found a good pair
Note that if we do not care about finding a good subset, and only about its existence, we do not need to care about the state array.