2

You are given a list of prices (non-negative integers) $$ P=<P_1, P_2, ..., P_n> $$, where the prices $P_i$ appear in non-decreasing order. Moreover, you have access to an oracle that can be queried with an integer $a$ and returns "yes" if and only if $a \le k$ for some unknown integer $k$. Each query to the oracle requires constant time.

The goal is that of finding a subset $S$ of $\{1, \dots,n\}$ such that the quantity $\sigma(S) = \sum_{i \in S} P_i$ is at most $k$ and $\sigma(S)$ is maximized.

Is there a way to solve this problem in $O(n)$ time?

For example, if $$ P=<P_1=3, P_2=5, P_3=10> $$ and the unknown integer $k$ is $16$, the best choice of $S$ is $\{2,3\}$ and $\sigma(S) = 15$.

Steven
  • 29,724
  • 2
  • 29
  • 49
John19
  • 63
  • 4

1 Answers1

4

There is no polynomial-time algorithm for your problem, unless $\mathsf{P}=\mathsf{NP}$.

Suppose that such an algorithm $A$ exists. Then we can use $A$ to solve the subset-sum problem (which is known to be $\mathsf{NP}$-complete) in polynomial time. Let $\langle X, t\rangle$ be an instance of subset-sum where $X = \langle X_1, \dots, X_n\rangle$ is a collection of integers and $t$ is the target integer.

We pick $P=X$ and $k=t$ and implement the oracle that decides whether $a \le k$ in the straightforward way. Then we simulate $A$ on input $P$ using the above oracle to compute $\sigma(S)$ in polynomial time.

If there exists a subset of $X$ of total sum $t$, then clearly the returned set $S$ is such that $\sigma(S)=t$. If there is no such subset of $X$, then all choices of $S' \subseteq \{1, \dots, n\}$ are such that $\sigma(S')$ is either strictly smaller or strictly larger than $t$. This shows that the returned set $S$ satisfies $\sigma(S) < t$.

We then answer "yes" to the subset sum instance if and only if $\sigma(S) = t$.

Steven
  • 29,724
  • 2
  • 29
  • 49