VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-position-element-sorted-array-infinite-numbers/

⇱ Find position of an element in a sorted array of infinite numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find position of an element in a sorted array of infinite numbers

Last Updated : 28 Aug, 2025

Given a sorted array arr[] of infinite numbers. The task is to search for an element k in the array.

Examples:

Input: arr[] = [3, 5, 7, 9, 10, 90, 100, 130, 140, 160, 170], k = 10
Output:4
Explanation: 10 is at index 4 in array.

Input: arr[] = [2, 5, 7, 9], k = 3
Output: -1
Explanation: 3 is not present in array.

[Expected Approach] Using Recursive Binary Search

Since the array is sorted, the first thing that comes to apply is binary search, but the problem here is that we don’t know the size of the array. If the array is infinite, we don't have proper bounds to apply binary search.

So in order to find the position of the key, first we find bounds and then apply a binary search algorithm. Let the low be pointing to the 1st element and the high pointing to the 2nd element of the array, Now compare the key with the high index element.

  • if it is greater than the high index element then copy the high index in the low index and double the high index. 
  • if it is smaller, then apply binary search on high and low indices found.

low and high will follow following kind of pattern:

  • l = 0, h = 1
  • l = 1, h = 2
  • l = 2, h = 4
  • l = 4, h = 8

So we can observe that over range is doubling every turn so it will take at most log(k) time.

Note: While doubling the value of h, there is possibility that h crosses the size of array, but we are considering the array to be infinite, and it is not possible to take an infinite array as input. Thus we might get out of bound error (which is not possible for infinite array).


Output
4

Time Complexity: O(log p), where p is the index of the target key. The algorithm first doubles the search range (O(log p)), then performs binary search in that range (O(log p)), giving an overall complexity of O(log p).
Auxiliary Space: O(log p), due to the recursive calls of the binary search.

[Alternate Approach] Using Iterative Binary Search

Approach is fundamentally similar to previous one. Difference is only in the way how we find bound:

  • Since array is sorted we apply binary search but the length of array is infinite so that we take start = 0 and end = 1 .
  • After that check value of target is greater than the value at end index, if it is true then change newStart = end + 1  and newEnd = end + (end - start +1)*2 and apply binary search .
  • Otherwise , apply binary search in the old index values.

low and high will follow following kind of pattern:

  • l = 0, h = 1
  • l = 2, h = 5
  • l = 6, h = 13
  • l = 14, h = 29

So we can observe that over range is doubling every turn so it will take at most log(k) time.

Note: We can see that this series grows faster than the previous approach. So in some cases this might be more optimal. Also, there is possibility that end crosses the size of array, but we are considering the array to be infinite, and it is not possible to take an infinite array as input. Thus we might get out of bound error (which is not possible for infinite array).


Output
10

Time Complexity: O(log p), where p is the index of the target key. The algorithm first doubles the search range (O(log p)), then performs binary search in that range (O(log p)), giving an overall complexity of O(log p).
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: