If you are given an unsorted list of n numbers, how long will it take to determine whether there are three numbers x, y, and z in the list such that xy = z?
If you are given an unsorted list of n numbers, how long will it take to determine...
683 Views Asked by user277825 https://math.techqa.club/user/user277825/detail AtThere are 2 best solutions below
On
All values are assumed positive. (If not, positive and negative sublists can be handled separately.)
Sort the given list by increasing values, in time $O(n\log(n))$.
Then take a value $z$ from the array and initialize two indexes, $i_x$ and $i_y$, at the beginning and after then end of the array, corresponding to the elements $x$ and $y$. By convention, past the array $y=\infty$.
Ensure that $i_y$ is the smallest index such that $xy\ge z$ holds, by decrementing $i_y$ if necessary. If in addition $xy=z$, you are done for this $z$.
Repeatedly increment $i_x$ and decrement $i_y$ accordingly as long as $xy\ge z$. In the end, $i_x$ and $i_y$ will meet. If the condition $xy=z$ was never established, the search for this $z$ failed.
Thanks to monotonicity, the search for a $z$ takes time $O(n)$. Hence for all $z$, $O(n^2)$.
L= [3, 4, 5, 6, 21, 56, 78, 126, 280, 752]
L.append(999999)
for z in range(len(L) - 1):
x= 0; y= len(L)
while x < y:
while x < y and L[x] * L[y - 1] >= L[z]:
y-= 1
if L[x] * L[y] == L[z]:
print L[x], "*", L[y], "=", L[z]
x+= 1
6 * 21 = 126
5 * 56 = 280
$O(n^2 \log n)$ is an upper bound. Your question says "numbers", which I'll assume means integers (because round-off worries complicate matters, though basically the same algorithm works). Also, your question doesn't require $x$ or $y$ to be unequal to $z$, nor does it require $x \neq y$ (more precisely, doesn't require that they be at different positions in the list $a$: $i \neq j$ where $x = a[i], y = a[j]$, which is sensible in case $a$ is e.g.
(2, 4). Finally, I'll assume the list contains nonnegative integers. Generalization to signed integers is straightforward.Here's a function which takes as parameter a list (array)
aof nonnegative integers. Ifacontains valuesx, y, zsuch thatx * y = z, the function returns a triple(i, j, k)such thatx == a[i], y == a]j], z == a[k]. Ifacontains no such values, the function returnsNone.Convention: assume indexes start at
0, soa = a[0:n]wheren = length(a)(soahas no element at indexn).Assume that there's an existing function
binsearch(arr, bottom, top, val)which takes a sorted arrayarr, integer indexesbottom, top, and a valueval. This function returns an indexjwithbottom <= j < topifarr[k] == val, otherwise it returns-1ifvalis not found ina[bottom:top]. Assume also that it implements binary search properly, so its time complexity is $O(\log_2 (top-bottom))$.We sort
afirst, so that we can binary-search for quotientsa[k] // a[i]. We can assume thatsortedimplements a sorting algorithm that's at worst $n^2$. Although we could assume better performance — $O(n \log n)$ by using, say, mergesort — for worst-case analysis it's unnecessary: the main loop offind_tripleis worse than quadratic, so any not-insane sort will do. (Note: the above is valid Python, though you can just take it to be pseudocode. Python's builtinsortedfunction uses quicksort. I have a working version offind_triple, including a workingbinsearch.)Given
k in range(n), clearly the inner loop takesO(i log i). So the runtime of the main loop takes a constant factor times $$ \begin{align} \Sigma_{k=1}^n k \log k &\le \log n \: \Sigma_{k=1}^n k \\ &= \log n \frac {n (n+1)} 2 \\ &= O(n^2 \log n) \text{.} \end{align} $$ This dominates the time required to sort, so it's an upper bound for the whole function.