In a game, your objective is to guess a number from the range 1 to n. You are allowed to make a maximum of ⌈log n⌉ guesses per round. Over the course of m rounds (where m < n), you record the correct answers in an array M[i], with i starting from 1 to m. Initially, designing an algorithm that runs in O(m*log(n)) time is straightforward. However, a new condition has been introduced: M[i] ≤ M[j] for all i > j, which means the answers are in descending order. The task is to find an algorithm that operates in O(n) time, given this modified condition.
Based on the condition M[i] ≤ M[j] for all i > j, I can narrow down the range between 1 to n. But in the worst case, I still cannot get O(n). Also, you cannot brutal force to guess the numbers, there is a limit on it.
Some clarifications: 1.When you guess, you are told it's too large or too small. (it's a classic binary search question) 2.The answers are in a descending order. 3.The limit of guessing times in each round is ⌈log n⌉, not log(n) + 1, sorry for my mistakes in the previous statement.
Let me give an example, hope this helps.
Let me know if you need more info.
Thanks in advance!
Inputs:
Goal: recreate M in sequence by making at most ⌈log n⌉ guesses per index, with a running time of O(n). I.e. we can't directly access M, but just guess a value of the first undetermined index and learn whether our guess is correct, or too big, or too small.
Notation:
Procedure to determine M[i] given M[i-1]:
First Constraint Analysis: guess each number in at most k = ⌈log n⌉ steps.
case 1 steps: 1
case 2 steps: M[i] - M[i-1] + 1 <= k
case 3: Binary search will take at most ⌈log n⌉ - 1 steps in the range 1 to n - ⌈log n⌉. Given that, this takes at most k steps (the initial guess, plus at most k-1 for binary search).
So this satisfies the first constraint, that each number is guessed in at most k steps.
Second Constraint Analysis: The algorithm should be O(n).
Let's say G[i] is the number of guesses we make to determine M[i]. Note that for all three cases, G[i] <= M[i-1] - M[i] + 1. In other words, if successive values of M differ by r, we will determine the latter value in at most r+1 guesses (never more than k, but we don't need that fact for this portion).
But the cumulative difference across all of M is at most n-1, and |M| < n, so the total number of guesses must be <= (n-1) + |M| < (n-1) + n = 2n-1 = O(n).