Find position of an element in a sorted array of infinite numbers (Galloping Search)

Medium Binary Search Search on Answer Range Original on GeeksforGeeks

Reported atAmazonAdobe

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.

00:00
Testcase

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.

ApproachTimeSpace
Linear scanO(p)O(1)
Optimal (galloping / exponential search)O(log p)O(1)

1Linear scan

TimeO(p)p is the position of the target.
SpaceO(1)

Walk from index 0 until you reach a value >= target.

  1. i = 0; while arr[i] < target: i++.
  2. 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;
    }
}

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].