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, Python, C++ and JavaScript, 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).
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (3 examples, or your own). Submit also runs 5 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
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 code in Java, Python, C++ and JavaScript.
| 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;
}
}class Solution:
def search(self, arr: 'ArrayReader', target: int) -> int:
i = 0
while arr.get(i) < target:
i += 1
return i if arr.get(i) == target else -1class Solution {
public:
int search(ArrayReader& arr, int target) {
int i = 0;
while (arr.get(i) < target) i++;
return arr.get(i) == target ? i : -1;
}
};function search(arr, target) {
let 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;
}
}class Solution:
def search(self, arr: 'ArrayReader', target: int) -> int:
lo, hi = 0, 1
while arr.get(hi) < target:
lo = hi
hi *= 2
while lo <= hi:
mid = lo + (hi - lo) // 2
v = arr.get(mid)
if v == target:
return mid
if v < target:
lo = mid + 1
else:
hi = mid - 1
return -1class Solution {
public:
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;
}
};function search(arr, target) {
let lo = 0, hi = 1;
while (arr.get(hi) < target) {
lo = hi;
hi *= 2;
}
while (lo <= hi) {
const mid = lo + Math.floor((hi - lo) / 2);
const 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].