How can I reverse the triangular array equation $k = (i - 1)(n - \frac{i}{2}) + j - i$ to find the $\{i, j\}$ pair that results in $k$?

438 Views Asked by At

I am trying to solve the problem below from the MMDS textbook. I can plug the equation into Excel to see that $i=7,\text{ }j=8$ is the solution, but I haven't been able to reverse the equation.

$\mathbf{Exercise\,\ 6.2.1:}$ If we use a triangular matrix to count pairs, and $n$, the number of items, is $20$, what pair's count is in $a[100]?$

For a bit of context, here's what the book has to say about triangular matrices:

The Triangular-Matrix Method

Even after coding items as integers, we still have the problem that we must count a pair $\{i,j\}$ in only one place. For example, we could order the pair so that $i<j$, and only use the entry $a[i,j]$ in a two-dimensional array $a$. That strategy would make half the array useless. A more space-efficient way is to use a one-dimensional triangular array. We store in $a[k]$ the count for the pair $\{i,j\}$, with $1\le i<j\le n$, where $$k=(i-1)\Big(n-\frac{i}{2}\Big)+j-i$$ The result of this layout is that the pairs are stored in lexicographic order, that is first $\{1,2\},\:\{1,3\},\ldots,\{1,n\}$, then $\{2,3\},\:\{2,4\},\ldots,\{2,n\}$, and so on, down to $\{n-2,n-1\},\:\{n-2,n\}$, and finally $\{n-1,n\}$.

Here are the steps I took:

$$ \begin{align} k &= (i - 1)(n - \frac{i}{2}) + j - i \\ k &= ni - \frac{i^2}{2} - n + \frac{i}{2} + j - i \\ 100 &= 20i - \frac{i^2}{2} - 20 + \frac{i}{2} + j - i \\ 120 &= 19.5i - \frac{i^2}{2} + j \end{align} $$

But that already seems incorrect because plugging in the values of $i=7,\text{ }j=8$ into the last line of the work shown above doesn't check out, but I know that $i=7,\text{ }j=8$ are correct because, as expected:

$$ \begin{align} (7 - 1)(20 - \frac{7}{2})+8-7 &= \\ 6(20 - 3.5) + 1 &= 100 \\ \end{align} $$

How should I solve this? Am I just bad at algebra?


(EDIT)

Okay, I am bad at algebra. The above actually holds, but how do I finish solving for $i$ and $j$? I'm getting an unexpected answer.

$$120=19.5i-\frac{i^2}{2}+j$$

${}$

$$\begin{align} 0&=\left(-\frac{i^2}{2}\right)+19.5i+(j-120) \\ &=i^2-39i+(-2)(j-120) \\ &=i^2-39i+(-2j+240) \\[0.15ex] &=i^2-39i+(240-2j) \end{align}$$

${}$

$$\frac{39\mp\sqrt{1521-4(240-2j)}}{2}=0$$

${}$

$$\begin{align} \frac{\pm\sqrt{1521-4(240-2j)}}{2}&=-39 \\ 1521-4(240-2j)&=1521 \\ -4(240-2j)&=0 \\ 240-2j&=0 \\ 2j&=240 \\ j&=120\:\,??? \\ \end{align}$$

1

There are 1 best solutions below

0
On

You went wrong when you set $$\frac{39\mp\sqrt{1521-4(240-2j)}}{2}=0,$$ because the expression on the left-hand side is equal to $i$ (since you solved the preceeding quadratic equation for $i$), and by definition, $i$ can't be $0$.


Here's one way to do it, though I imagine better solutions exist.

Expanding on mathlove's comment, you can solve the equation $$i^2-39i+240-2j=0$$ for either $i$ or $j$ (though I would very strongly recommend solving it for $j$) and plug in integer values of the other variable until you find a pair that satisfies $1\le i\lt j\le n$. Solving the above equation for $j$ to get $$j=\frac{1}{2}i^2-\frac{39}{2}i+120$$ and starting with $i=1$, moving up by steps of $1$, the first (and only) solution you find is $i=7,j=8$.