I saw a question on an old exam in my university course of DSA and I am not sure if the question is far too easy or if I am missing something.
The question is :
Given an array A that consists of N integers such that for any 1ドル < i \leq N$ the following properties holds :
- $| A[i] - A[i-1] | \leq 1$
- $ A[N] > A[1] $
Given an element z such that A[1] $\leq$ z $\leq$ A[N], Write an efficient search algorithm and analyze its complexity.
I started thinking about this problem and after a few minutes, I found that from the fact we have A[1] < A[N] and that the absolute value of the difference between 2 consecutive elements has to be at most one, we can deduce that our array must contain all the values between A[1] and A[N], therefore the algorithm should only check if z is between A[1] and A[N].
Am I missing something?
-
$\begingroup$ You need to find the index of the element, not whether it exists or not. $\endgroup$nir shahar– nir shahar2022年06月16日 19:59:46 +00:00Commented Jun 16, 2022 at 19:59
-
$\begingroup$ @nirshahar shahar Yes of course it could be increasing and then decreasing but eventually, it has to attain some value A[N] which has to be greater than A[1]. So we can say for sure that if z is an element between A[1] and A[N] it has to be in the array, am I missing something? $\endgroup$Yarin– Yarin2022年06月16日 20:01:22 +00:00Commented Jun 16, 2022 at 20:01
-
$\begingroup$ @nirshahar Gotcha, it really might be finding the index of the element, so maybe I can implement some variation of binary search here? EDIT: the thing is that a given element between A[1] and A[N] could appear as many times as we want because the values can oscillate up and down as long as in the end we will attain A[N], guess I'll have to think about it more $\endgroup$Yarin– Yarin2022年06月16日 20:07:38 +00:00Commented Jun 16, 2022 at 20:07
-
1$\begingroup$ The question is incomplete: does "search" mean to find a single occurrence, or all of them ? $\endgroup$user16034– user160342022年06月17日 08:20:11 +00:00Commented Jun 17, 2022 at 8:20
-
$\begingroup$ By the way, your "easy solution" to the existence problem is wrong: z could be in the array even if not between A[1] and A[N]. $\endgroup$user16034– user160342022年06月17日 08:21:35 +00:00Commented Jun 17, 2022 at 8:21
2 Answers 2
Lets say a[1] = 1, a[N = 1000] = 2, and z = 450. The first array element that could equal z is a[450], and the last one is a[552]. Since 450 ≤ 552, there could be a solution.
So you have reduced the range. Now check the values a[450] and a[552]. Either one of them is equal to z, or you can reduce the range again. Now the worst case is that you have a large range of values equal to z-1, and there might or might not be one equal to z in the range. Solving this will require linear time. Bad example:
a[1] = 1, a[2] .. a[N = 1000] = 2 except perhaps a[k] = 3, and z = 3. You have to check every value.
-
$\begingroup$ Please note that they're asking about a given z which satisfies A[1] $\leq$ z $\leq$ A[N] $\endgroup$Yarin– Yarin2022年06月17日 09:58:12 +00:00Commented Jun 17, 2022 at 9:58
The proposed approach by @gnasher729 is true. Maybe the following commands clarify it.
Let L=1 and U=N for the lower and upper range of the search area in array A.
- Repeat the following commands:
- If L>U then Z does not exist in array A and terminate.
- If Z==A[L] or Z==A[U] then we found Z and terminate.
- Else, reduce the search area by L=Z-A[L] and U=A[U]-Z; i.e. Z cannot be found before index Z-A[L] and after index A[U]-Z.