I am trying to calculate some thing and I got lost.
I have sorted(low to high) array of $N$ numbers, with first number $K$ and numbers sum of $S$. Assuming that there are no duplicated numbers and their growth is linear, how to calculate the index of a number based on its value.
For example:
- Input: array = $[8, 10, 12, 14, 16, 18, 20], value = 10, k = 8, S = 98, N = 7$
- Output: 1
Oh, this is a fun one. start by finding an expression for the sum, $\mathcal{S}$.
Let $C$ be the constant difference between consecutive items in the sequence. Then the sum is: $$\begin{align}\mathcal{S} &= (K + 0C) + (K + 1C) + (K + 2C) + \cdots + \left(K + (N-1)C\right) \\ &= NK + C\frac{(N-1)N}{2}\end{align}$$ Solve for $C$. $$C = \frac{2(S - NK)}{N(N-1)}$$
and we know that $a_i$ , the element in the sequence with index $i$, is given by $$a_i = K + iC$$
So, $$ i = \frac{a_i - K}{C} = \frac{(a_i - K)N(N-1)}{2(S-NK)}$$