0% found this document useful (0 votes)
30 views3 pages

Binary Search For Infinite Array

The document discusses two approaches to find the index of an element in an infinite sorted array: a naive approach that iterates through the entire array with O(n) time complexity, and a more efficient approach that uses binary search with O(log n) time complexity after narrowing the search space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

Binary Search For Infinite Array

The document discusses two approaches to find the index of an element in an infinite sorted array: a naive approach that iterates through the entire array with O(n) time complexity, and a more efficient approach that uses binary search with O(log n) time complexity after narrowing the search space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question :

You are given an infinite sized array which is sorted and you need to find out the index of the element
that will be given to you by us, let say  x=700.

Naive Approach
Itterating over the array and searching the element , in this approach 2 cases arises :

1. if element is found - return its index.


2. While traversing over the array an incident will come when the previous element - smaller
than the desired element and the next element - greater than the desired element.In such
case you will return -1. Indicating no such element is present.
Time complexity for naive approach - O(positions)
Space complexity - O(1)

Efficient Approach
Make a seprate case for the 0th index and start from index=1 and each time multiply it by 2 search
for the element is you encounter a case where current element - greater than the desired element
then you will get a Bounded region in which 2 things are for sure that :

1. The element at ( i / 2 )th index is smaller than the desired element.


2. The element at ( i ) is greater than the desired element
In such case break the infinte loop and apply binary search .
Time complexity - O(Log(position))
Space complexity - O(1)

You might also like