Find the index of first 1 in an infinite sorted array of 0s and 1s
Given an infinite sorted array consisting 0s and 1s. The problem is to find the index of first ‘1’ in that array. As the array is infinite, therefore it is guaranteed that number ‘1’ will be present in the array.
Examples:
Input : arr[] = {0, 0, 1, 1, 1, 1}
Output : 2
Input : arr[] = {1, 1, 1, 1,, 1, 1}
Output : 0
Approach: The problem is closely related to the problem of finding position of an element in a sorted array of infinite numbers. As the array is infinite, therefore we do not know the upper and lower bounds between which we have to find the occurrence of first ‘1’.
Below is an algorithm to find the upper and lower bounds.
Algorithm:
posOfFirstOne(arr)
Declare l = 0, h = 1
while arr[h] == 0
l = h
h = 2*h;
return indexOfFirstOne(arr, l, h)
}
Here h and l are the required upper and lower bounds. indexOfFirstOne(arr, l, h) is used to find the index of occurrence of first ‘1’ between these two bounds. Refer this post.
C++
#include <bits/stdc++.h>
using namespace std;
int indexOfFirstOne( int arr[], int low, int high)
{
int mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == 1 &&
(mid == 0 || arr[mid - 1] == 0))
break ;
else if (arr[mid] == 1)
high = mid - 1;
else
low = mid + 1;
}
return mid;
}
int posOfFirstOne( int arr[])
{
int l = 0, h = 1;
while (arr[h] == 0) {
l = h;
h = 2 * h;
}
return indexOfFirstOne(arr, l, h);
}
int main()
{
int arr[] = { 0, 0, 1, 1, 1, 1 };
cout << "Index = "
<< posOfFirstOne(arr);
return 0;
}
|
Java
Python3
def indexOfFirstOne(arr, low, high) :
while (low < = high) :
mid = (low + high) / / 2
if (arr[mid] = = 1 and (mid = = 0 or
arr[mid - 1 ] = = 0 )) :
break
elif (arr[mid] = = 1 ) :
high = mid - 1
else :
low = mid + 1
return mid
def posOfFirstOne(arr) :
l = 0
h = 1
while (arr[h] = = 0 ) :
l = h
h = 2 * h
return indexOfFirstOne(arr, l, h)
arr = [ 0 , 0 , 1 , 1 , 1 , 1 ]
print ( "Index = " , posOfFirstOne(arr))
|
C#
using System;
class GFG {
public static int indexOfFirstOne( int [] arr,
int low, int high)
{
int mid = 0;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == 1 && (mid == 0 || arr[mid - 1] == 0))
break ;
else if (arr[mid] == 1)
high = mid - 1;
else
low = mid + 1;
}
return mid;
}
public static int posOfFirstOne( int [] arr)
{
int l = 0, h = 1;
while (arr[h] == 0) {
l = h;
h = 2 * h;
}
return indexOfFirstOne(arr, l, h);
}
public static void Main()
{
int [] arr = { 0, 0, 1, 1, 1, 1 };
Console.Write( "Index = " + posOfFirstOne(arr));
}
}
|
PHP
<?php
function indexOfFirstOne( $arr , $low , $high )
{
$mid ;
while ( $low <= $high )
{
$mid = ( $low + $high ) / 2;
if ( $arr [ $mid ] == 1 and
( $mid == 0 or $arr [ $mid - 1] == 0))
break ;
else if ( $arr [ $mid ] == 1)
$high = $mid - 1;
else
$low = $mid + 1;
}
return ceil ( $mid );
}
function posOfFirstOne( $arr )
{
$l = 0; $h = 1;
while ( $arr [ $h ] == 0)
{
$l = $h ;
$h = 2 * $h ;
}
return indexOfFirstOne( $arr ,
$l , $h );
}
$arr = array (0, 0, 1, 1, 1, 1);
echo "Index = " ,
posOfFirstOne( $arr );
?>
|
Javascript
<script>
function indexOfFirstOne(arr, low, high)
{
var mid;
while (low <= high) {
mid = parseInt((low + high) / 2);
if (arr[mid] == 1 &&
(mid == 0 || arr[mid - 1] == 0))
break ;
else if (arr[mid] == 1)
high = mid - 1;
else
low = mid + 1;
}
return mid;
}
function posOfFirstOne(arr)
{
var l = 0, h = 1;
while (arr[h] == 0) {
l = h;
h = 2 * h;
}
return indexOfFirstOne(arr, l, h);
}
var arr = [0, 0, 1, 1, 1, 1];
document.write( "Index = "
+ posOfFirstOne(arr));
</script>
|
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).
Last Updated :
18 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...