Find position of an element in a sorted array of infinite numbers
Last Updated :
18 Mar, 2023
Suppose you have a sorted array of infinite numbers, how would you search an element in the array?
Source: Amazon Interview Experience.
Since array is sorted, the first thing clicks into mind is binary search, but the problem here is that we don’t know size of array.
If the array is infinite, that means we don’t have proper bounds to apply binary search. So in order to find position of key, first we find bounds and then apply binary search algorithm.
Let low be pointing to 1st element and high pointing to 2nd element of array, Now compare key with high index element,
->if it is greater than high index element then copy high index in low index and double the high index.
->if it is smaller, then apply binary search on high and low indices found.
Below are implementations of above algorithm
C++
#include<bits/stdc++.h>
using namespace std;
int binarySearch( int arr[], int l, int r, int x)
{
if (r>=l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
int findPos( int arr[], int key)
{
int l = 0, h = 1;
int val = arr[0];
while (val < key)
{
l = h;
h = 2*h;
val = arr[h];
}
return binarySearch(arr, l, h, key);
}
int main()
{
int arr[] = {3, 5, 7, 9, 10, 90, 100, 130,
140, 160, 170};
int ans = findPos(arr, 10);
if (ans==-1)
cout << "Element not found" ;
else
cout << "Element found at index " << ans;
return 0;
}
|
Java
class Test
{
static int binarySearch( int arr[], int l, int r, int x)
{
if (r>=l)
{
int mid = l + (r - l)/ 2 ;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid- 1 , x);
return binarySearch(arr, mid+ 1 , r, x);
}
return - 1 ;
}
static int findPos( int arr[], int key)
{
int l = 0 , h = 1 ;
int val = arr[ 0 ];
while (val < key)
{
l = h;
if ( 2 *h < arr.length- 1 )
h = 2 *h;
else
h = arr.length- 1 ;
val = arr[h];
}
return binarySearch(arr, l, h, key);
}
public static void main(String[] args)
{
int arr[] = new int []{ 3 , 5 , 7 , 9 , 10 , 90 ,
100 , 130 , 140 , 160 , 170 };
int ans = findPos(arr, 10 );
if (ans==- 1 )
System.out.println( "Element not found" );
else
System.out.println( "Element found at index " + ans);
}
}
|
Python3
def binary_search(arr,l,r,x):
if r > = l:
mid = l + (r - l) / / 2
if arr[mid] = = x:
return mid
if arr[mid] > x:
return binary_search(arr,l,mid - 1 ,x)
return binary_search(arr,mid + 1 ,r,x)
return - 1
def findPos(a, key):
l, h, val = 0 , 1 , arr[ 0 ]
while val < key:
l = h
h = 2 * h
val = arr[h]
return binary_search(a, l, h, key)
arr = [ 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 ]
ans = findPos(arr, 10 )
if ans = = - 1 :
print ( "Element not found" )
else :
print ( "Element found at index" ,ans)
|
C#
using System;
class GFG {
static int binarySearch( int []arr, int l,
int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l,
mid-1, x);
return binarySearch(arr, mid+1,
r, x);
}
return -1;
}
static int findPos( int []arr, int key)
{
int l = 0, h = 1;
int val = arr[0];
while (val < key)
{
l = h;
h = 2 * h;
val = arr[h];
}
return binarySearch(arr, l, h, key);
}
public static void Main()
{
int []arr = new int []{3, 5, 7, 9,
10, 90, 100, 130, 140, 160, 170};
int ans = findPos(arr, 10);
if (ans == -1)
Console.Write( "Element not found" );
else
Console.Write( "Element found at "
+ "index " + ans);
}
}
|
PHP
<?php
function binarySearch( $arr , $l ,
$r , $x )
{
if ( $r >= $l )
{
$mid = $l + ( $r - $l )/2;
if ( $arr [ $mid ] == $x )
return $mid ;
if ( $arr [ $mid ] > $x )
return binarySearch( $arr , $l ,
$mid - 1, $x );
return binarySearch( $arr , $mid + 1,
$r , $x );
}
return -1;
}
function findPos( $arr , $key )
{
$l = 0; $h = 1;
$val = $arr [0];
while ( $val < $key )
{
$l = $h ;
$h = 2 * $h ;
$val = $arr [ $h ];
}
return binarySearch( $arr , $l ,
$h , $key );
}
$arr = array (3, 5, 7, 9, 10, 90, 100,
130, 140, 160, 170);
$ans = findPos( $arr , 10);
if ( $ans ==-1)
echo "Element not found" ;
else
echo "Element found at index " , $ans ;
?>
|
Javascript
<script>
function binarySearch(arr, l, r, x)
{
if (r>=l)
{
let mid = l + Math.floor((r - l)/2);
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
function findPos(arr, key)
{
let l = 0, h = 1;
let val = arr[0];
while (val < key)
{
l = h;
h = 2*h;
val = arr[h];
}
return binarySearch(arr, l, h, key);
}
let arr = [3, 5, 7, 9, 10, 90, 100, 130,
140, 160, 170];
let ans = findPos(arr, 10);
if (ans==-1)
document.write( "Element not found" );
else
document.write( "Element found at index " + ans);
</script>
|
Output
Element found at index 4
Let p be the position of element to be searched. Number of steps for finding high index ‘h’ is O(Log p). The value of ‘h’ must be less than 2*p. The number of elements between h/2 and h must be O(p). Therefore, time complexity of Binary Search step is also O(Log p) and overall time complexity is 2*O(Log p) which is O(Log p).
Approach: The problem can be solved based on the following observation:
- 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.
Below are implementations of above algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
int binarySearch( int arr[], int target, int start, int end)
{
while (start <= end) {
int mid = start + (end - start) / 2;
if (target < arr[mid]) {
end = mid - 1;
}
else if (target > arr[mid]) {
start = mid + 1;
}
else {
return mid;
}
}
return -1;
}
int findPos( int arr[], int target)
{
int start = 0;
int end = 1;
while (target > arr[end]) {
int temp = end + 1;
end = end + (end - start + 1) * 2;
start = temp;
}
return binarySearch(arr, target, start, end);
}
int main()
{
int arr[]
= { 3, 5, 7, 9, 10, 90, 100, 130, 140, 160, 170 };
int target = 10;
int ans = findPos(arr, target);
if (ans == -1)
cout << "Element not found" ;
else
cout << "Element found at index " << ans;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static int findPos( int [] arr, int target)
{
int start = 0 ;
int end = 1 ;
while (target > arr[end]) {
int temp = end + 1 ;
end = end + (end - start + 1 ) * 2 ;
start = temp;
}
return binarySearch(arr, target, start, end);
}
static int binarySearch( int [] arr, int target,
int start, int end)
{
while (start <= end) {
int mid = start + (end - start) / 2 ;
if (target < arr[mid]) {
end = mid - 1 ;
}
else if (target > arr[mid]) {
start = mid + 1 ;
}
else {
return mid;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int [] arr = { 3 , 5 , 7 , 9 , 10 , 90 ,
100 , 130 , 140 , 160 , 170 };
int target = 10 ;
int ans = findPos(arr, target);
if (ans == - 1 )
System.out.println( "Element not found" );
else
System.out.println( "Element found at index "
+ ans);
}
}
|
Python3
import sys
def findPos(arr, target):
start = 0
end = 1
while target > arr[end]:
temp = end + 1
end = end + (end - start + 1 ) * 2
start = temp
return binarySearch(arr, target, start, end)
def binarySearch(arr, target, start, end):
while start < = end:
mid = start + (end - start) / / 2
if target < arr[mid]:
end = mid - 1
elif target > arr[mid]:
start = mid + 1
else :
return mid
return - 1
if __name__ = = '__main__' :
arr = [ 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 ]
target = 10
ans = findPos(arr, target)
if ans = = - 1 :
print ( "Element not found" )
else :
print ( "Element found at index" , ans)
|
Javascript
<script>
function findPos( arr, target)
{
let start = 0;
let end = 1;
while (target > arr[end]) {
let temp = end + 1;
end = end + (end - start + 1) * 2;
start = temp;
}
return binarySearch(arr, target, start, end);
}
function binarySearch(arr, target, start, end)
{
while (start <= end) {
let mid = start + Math.floor((end - start) / 2);
if (target < arr[mid]) {
end = mid - 1;
}
else if (target > arr[mid]) {
start = mid + 1;
}
else {
return mid;
}
}
return -1;
}
let arr = [3, 5, 7, 9, 10, 90, 100, 130,
140, 160, 170];
let ans = findPos(arr, 10);
if (ans==-1)
document.write( "Element not found" );
else
document.write( "Element found at index " + ans);
</script>
|
C#
using System;
class GFG
{
static int FindPos( int [] arr, int target)
{
int start = 0;
int end = 1;
while (target > arr[end])
{
int temp = end + 1;
end = end + (end - start + 1) * 2;
start = temp;
}
return BinarySearch(arr, target, start, end);
}
static int BinarySearch( int [] arr, int target, int start, int end)
{
while (start <= end)
{
int mid = start + (end - start) / 2;
if (target < arr[mid])
{
end = mid - 1;
}
else if (target > arr[mid])
{
start = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
public static void Main()
{
int [] arr = { 3, 5, 7, 9, 10, 90,
100, 130, 140, 160, 170 };
int target = 10;
int ans = FindPos(arr, target);
if (ans == -1)
Console.WriteLine( "Element not found" );
else
Console.WriteLine( "Element found at index "
+ ans);
}
}
|
Output
Element found at index 4
Time Complexity: O(logN)
Auxiliary Space: O(1)
Please Login to comment...