i want to write a program to do this it will take input n and index n is the size of table in this example it is equal to 6 and index like 11 so the output must be 3,5 if index = 9 the output is 2,6
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3857 |
| 2 | jiangly | 3810 |
| 3 | maroonrk | 3534 |
| 4 | tourist | 3528 |
| 5 | Kevin114514 | 3510 |
| 6 | turmax | 3411 |
| 7 | Um_nik | 3387 |
| 8 | Radewoosh | 3367 |
| 9 | heuristica | 3322 |
| 10 | strapple | 3317 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 158 |
| 2 | maspy | 150 |
| 3 | Um_nik | 146 |
| 4 | Errichto | 139 |
| 5 | nik_exists | 138 |
| 6 | adamant | 136 |
| 7 | maroonrk | 134 |
| 8 | DNR | 133 |
| 9 | Dominater069 | 131 |
| 9 | AmShZ | 131 |
i want to write a program to do this it will take input n and index n is the size of table in this example it is equal to 6 and index like 11 so the output must be 3,5 if index = 9 the output is 2,6
| Name |
|---|



From your image, this is the best code that I can muster.
And now you just print which index
iandjforl[i][j]that you need.Well assuming the table has size of N then we have rows of size N, N - 1, N - 2...1. Let's assume we're given index K — if we are able to find the row of that cell, then we'll be done (refer to the end of the comment). So let's try to find that row.
We can notice that the amount of cells in the first P rows sums up to (arithmetic progression):
If we assume the cell we're looking for is at row X then we know that X is the largest number such that the sum of the first X - 1 rows is strictly less than K. Formally, we're looking for the largest X in [1, N] such that:
Binary search solution
Since the left side obviously increases as we increase X, we can simply use binary search to find the value of X. This gives us an O(logN) solution.
Constant solution
We can try to work with the equation a bit:
We can easily treat the left side as a quadratic equation with respect to X. We can then find the roots of the equation — call them X1 and X2 (X1 ≤ X2). From the properties of quadratic functions we know that the function has negative value for X in ( - inf, X1) and (X2, + inf). We also know that X is in [1, N], so we can easily find the values for which the inequality holds and X is a proper row. So we can constantly find the largest X that is okay.
This solution has complexity of O(1), assuming finding the roots of a quadratic equation is constant.
Finding the column once we know the row
Finally, once we've found that row X, let's find the column Y. We know that the first cell in row X starts at column X + 1. We also know that there are
cells in rows above ours. So to the left of our cell we'll need
more cells.
Finally we get that:
which takes O(1) to compute.
P.S.
Your picture looks quite horrible and you should've taken the time to explain the actual statement. You also lack any kind of punctuation which makes it hard to understand your question. You also don't have any constraints on N so it is not clear if the obvious O(K) solution is sufficient. Please take more time when asking questions the next time.
(I haven't coded a solution so any formulas above may be wrong. The idea, however, should be correct)