Find position of an element in a sorted array of infinite numbers (Galloping Search)
Find position of an element in a sorted array of infinite numbers (Galloping Search) is a medium binary search problem solved with the search on answer range pattern.
The best approach, optimal (galloping / exponential search), runs in O(log p) time and O(1) space.
Below are 2 approaches in Java, from linear scan up.
Problem
A sorted array is so large that its length is unknown (treat it as infinite). You can only read values by index. Find the position of target, or return -1.
Examples
Example 1
- Input
arr = [3, 5, 7, 9, 10, 90, 100, 130, 140, 160, ...], target = 10- Output
4
Example 2
- Input
same array, target = 8- Output
-1
Constraints
- The array is sorted and too large to know its length.
- You may only read arr.get(i).
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.
| Approach | Time | Space |
|---|---|---|
| Linear scan | O(p) | O(1) |
| Optimal (galloping / exponential search) | O(log p) | O(1) |
1Linear scan
O(p)p is the position of the target.O(1)Walk from index 0 until you reach a value >= target.
- i = 0; while arr[i] < target: i++.
- Return i if arr[i] == target, else -1.
class Solution {
interface ArrayReader { int get(int i); }
int search(ArrayReader arr, int target) {
int i = 0;
while (arr.get(i) < target) i++;
return arr.get(i) == target ? i : -1;
}
}2Optimal (galloping / exponential search)
O(log p)log p doublings, then a binary search over a range of size p.O(1)Find bounds by doubling hi until arr.get(hi) >= target; the previous hi is a safe lo. Then run an ordinary binary search on [lo, hi].
- lo = 0, hi = 1; while arr.get(hi) < target: lo = hi; hi *= 2.
- Binary search on [lo, hi].
class Solution {
interface ArrayReader { int get(int i); }
int search(ArrayReader arr, int target) {
int lo = 0, hi = 1;
while (arr.get(hi) < target) {
lo = hi;
hi *= 2;
}
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int v = arr.get(mid);
if (v == target) return mid;
if (v < target) lo = mid + 1; else hi = mid - 1;
}
return -1;
}
}Edge cases to test
- Target at index 0
- Target smaller than arr[0]
Hints
Hint 1
Double hi (1, 2, 4, 8, ...) until arr[hi] >= target. Then binary search inside [hi / 2, hi].
FAQ
What is the best time complexity for Find position of an element in a sorted array of infinite numbers (Galloping Search)?
Optimal (galloping / exponential search) runs in O(log p) time and O(1) extra space. log p doublings, then a binary search over a range of size p.
Which pattern does Find position of an element in a sorted array of infinite numbers (Galloping Search) use?
It is a binary search problem that uses the search on answer range pattern. Other problems with the same pattern: Pow(x, n) (Binary Exponentiation), Find Nth root of M, Sqrt(x).
Is there a brute force solution for Find position of an element in a sorted array of infinite numbers (Galloping Search)?
Yes. Linear scan takes O(p) time and O(1) space. Walk from index 0 until you reach a value = target.
Which edge cases should I test for Find position of an element in a sorted array of infinite numbers (Galloping Search)?
Target at index 0; Target smaller than arr[0].