Searching in an array where adjacent differ by at most k
Pattern: Traversal and in-place tricks
Neighbouring values differ by at most k, so the target is at least |arr[i] - x| / k steps away. Jump that far instead of stepping one by one.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
You are given an array of whole numbers in which every two neighbours differ by at most k (so
|arr[i] - arr[i+1]| <= k), plus a value x. Return the index of the first element equal to x, or -1 if there is
none. For example, in [4, 5, 6, 7, 6] with k = 1, the first 6 is at index 2.
This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name search,
the "first index" rule and the handling of k = 0 are this guide's own.
Think first
- At index
ithe value is 4 and you want 6. Withk = 1, what is the earliest index where a 6 could appear? - Is it safe to skip the positions in between? Why can't the target be hiding there?
- What should happen when
k = 0?
Hints
Brute force, and why it fails
Linear search checks every index and is always correct, in O(n). The jumping search is never slower and often much faster
when the values are far from x. Its worst case is still O(n), for example when every value is within k of x.
The key insight
The "neighbours differ by at most k" rule puts a speed limit on how fast values can change. That speed limit tells you how many positions you can safely skip.
Dry run
| x | 6 |
|---|---|
| k | 1 |
arr = [4, 5, 6, 7, 6], k = 1 and x = 6. Start at index 0.
The plan
i = 0
while i < n:
if arr[i] == x:
return i
jump = 1 if k == 0 else |arr[i] - x| div k
i = i + max(1, jump)
return -1
Complexity
- Time
O(n) worst case- Space
O(1)
Never more steps than linear search, and often far fewer. Only one index is stored.
Edge cases
xmissing: the jumps run past the end and the answer is -1.k = 0: every value is the same, so step one at a time (and findxat index 0, or nowhere).- The first occurrence is always the one returned, because a jump never skips a position where
xcould be.
I've built my logic, show the code
Java
class Solution {
public int search(int[] arr, int k, int x) {
int i = 0;
while (i < arr.length) {
if (arr[i] == x) {
return i;
}
// each step changes the value by at most k, so x is at least |arr[i] - x| / k steps away
int jump = (k == 0) ? 1 : Math.abs(arr[i] - x) / k;
i += Math.max(1, jump);
}
return -1;
}
}C++
class Solution {
public:
int search(vector<int>& arr, int k, int x) {
int i = 0;
int n = static_cast<int>(arr.size());
while (i < n) {
if (arr[i] == x) {
return i;
}
// each step changes the value by at most k, so x is at least |arr[i] - x| / k steps away
int jump = (k == 0) ? 1 : abs(arr[i] - x) / k;
i += max(1, jump);
}
return -1;
}
};Python
class Solution:
def search(self, arr, k, x):
i = 0
while i < len(arr):
if arr[i] == x:
return i
# each step changes the value by at most k, so x is at least |arr[i] - x| / k steps away
jump = 1 if k == 0 else abs(arr[i] - x) // k
i += max(1, jump)
return -1Common mistakes
- Jumping by
|arr[i] - x| / kwithout themax(1, ...)guard, which loops forever when the value is close tox. - Dividing by
kwhenk = 0. - Rounding the jump up, which can skip over the target.