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 infinte, 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)Declarel= 0,h= 1 whilearr[h]== 0 l = h h = 2*h; returnindexOfFirstOne(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++

`// C++ implementation to find the index of first 1 ` `// in an infinite sorted array of 0's and 1's ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find the index of first '1' ` `// binary search technique is applied ` `int` `indexOfFirstOne(` `int` `arr[], ` `int` `low, ` `int` `high) ` `{ ` ` ` `int` `mid; ` ` ` `while` `(low <= high) { ` ` ` `mid = (low + high) / 2; ` ` ` ` ` `// if true, then 'mid' is the index of first '1' ` ` ` `if` `(arr[mid] == 1 && ` ` ` `(mid == 0 || arr[mid - 1] == 0)) ` ` ` `break` `; ` ` ` ` ` `// first '1' lies to the left of 'mid' ` ` ` `else` `if` `(arr[mid] == 1) ` ` ` `high = mid - 1; ` ` ` ` ` `// first '1' lies to the right of 'mid' ` ` ` `else` ` ` `low = mid + 1; ` ` ` `} ` ` ` ` ` `// required index ` ` ` `return` `mid; ` `} ` ` ` `// function to find the index of first 1 in ` `// an infinite sorted array of 0's and 1's ` `int` `posOfFirstOne(` `int` `arr[]) ` `{ ` ` ` `// find the upper and lower bounds between ` ` ` `// which the first '1' would be present ` ` ` `int` `l = 0, h = 1; ` ` ` ` ` `// as the array is being considered infinite ` ` ` `// therefore 'h' index will always exist in ` ` ` `// the array ` ` ` `while` `(arr[h] == 0) { ` ` ` ` ` `// lower bound ` ` ` `l = h; ` ` ` ` ` `// upper bound ` ` ` `h = 2 * h; ` ` ` `} ` ` ` ` ` `// required index of first '1' ` ` ` `return` `indexOfFirstOne(arr, l, h); ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 0, 0, 1, 1, 1, 1 }; ` ` ` `cout << ` `"Index = "` ` ` `<< posOfFirstOne(arr); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA Code For Find the index of first 1 ` `// in an infinite sorted array of 0s and 1s ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find the index of first '1' ` ` ` `// binary search technique is applied ` ` ` `public` `static` `int` `indexOfFirstOne(` `int` `arr[], ` ` ` `int` `low, ` `int` `high) ` ` ` `{ ` ` ` `int` `mid=` `0` `; ` ` ` `while` `(low <= high) { ` ` ` `mid = (low + high) / ` `2` `; ` ` ` ` ` `// if true, then 'mid' is the index of ` ` ` `// first '1' ` ` ` `if` `(arr[mid] == ` `1` `&& ` ` ` `(mid == ` `0` `|| arr[mid - ` `1` `] == ` `0` `)) ` ` ` `break` `; ` ` ` ` ` `// first '1' lies to the left of 'mid' ` ` ` `else` `if` `(arr[mid] == ` `1` `) ` ` ` `high = mid - ` `1` `; ` ` ` ` ` `// first '1' lies to the right of 'mid' ` ` ` `else` ` ` `low = mid + ` `1` `; ` ` ` `} ` ` ` ` ` `// required index ` ` ` `return` `mid; ` ` ` `} ` ` ` ` ` `// function to find the index of first 1 in ` ` ` `// an infinite sorted array of 0's and 1's ` ` ` `public` `static` `int` `posOfFirstOne(` `int` `arr[]) ` ` ` `{ ` ` ` `// find the upper and lower bounds ` ` ` `// between which the first '1' would ` ` ` `// be present ` ` ` `int` `l = ` `0` `, h = ` `1` `; ` ` ` ` ` `// as the array is being considered ` ` ` `// infinite therefore 'h' index will ` ` ` `// always exist in the array ` ` ` `while` `(arr[h] == ` `0` `) { ` ` ` ` ` `// lower bound ` ` ` `l = h; ` ` ` ` ` `// upper bound ` ` ` `h = ` `2` `* h; ` ` ` `} ` ` ` ` ` `// required index of first '1' ` ` ` `return` `indexOfFirstOne(arr, l, h); ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `arr[] = { ` `0` `, ` `0` `, ` `1` `, ` `1` `, ` `1` `, ` `1` `}; ` ` ` `System.out.println(` `"Index = "` `+ ` ` ` `posOfFirstOne(arr)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnav Kr. Mandal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation to find the ` `# index of first 1 in an infinite ` `# sorted array of 0's and 1's ` ` ` `# function to find the index of first ` `# '1' binary search technique is applied ` `def` `indexOfFirstOne(arr, low, high) : ` ` ` ` ` `while` `(low <` `=` `high) : ` ` ` `mid ` `=` `(low ` `+` `high) ` `/` `/` `2` ` ` ` ` `# if true, then 'mid' is the index ` ` ` `# of first '1' ` ` ` `if` `(arr[mid] ` `=` `=` `1` `and` `(mid ` `=` `=` `0` `or` ` ` `arr[mid ` `-` `1` `] ` `=` `=` `0` `)) : ` ` ` `break` ` ` ` ` `# first '1' lies to the left of 'mid' ` ` ` `elif` `(arr[mid] ` `=` `=` `1` `) : ` ` ` `high ` `=` `mid ` `-` `1` ` ` ` ` `# first '1' lies to the right of 'mid' ` ` ` `else` `: ` ` ` `low ` `=` `mid ` `+` `1` ` ` ` ` `# required index ` ` ` `return` `mid ` ` ` `# function to find the index of first ` `# 1 in an infinite sorted array of 0's ` `# and 1's ` `def` `posOfFirstOne(arr) : ` ` ` ` ` `# find the upper and lower bounds between ` ` ` `# which the first '1' would be present ` ` ` `l ` `=` `0` ` ` `h ` `=` `1` ` ` ` ` `# as the array is being considered infinite ` ` ` `# therefore 'h' index will always exist in ` ` ` `# the array ` ` ` `while` `(arr[h] ` `=` `=` `0` `) : ` ` ` ` ` `# lower bound ` ` ` `l ` `=` `h ` ` ` ` ` `# upper bound ` ` ` `h ` `=` `2` `*` `h ` ` ` ` ` `# required index of first '1' ` ` ` `return` `indexOfFirstOne(arr, l, h) ` ` ` `# Driver program ` `arr ` `=` `[ ` `0` `, ` `0` `, ` `1` `, ` `1` `, ` `1` `, ` `1` `] ` `print` `( ` `"Index = "` `, posOfFirstOne(arr)) ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# Code For Find the index of first 1 ` `// in an infinite sorted array of 0's and 1's ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find the index of first '1' ` ` ` `// binary search technique is applied ` ` ` `public` `static` `int` `indexOfFirstOne(` `int` `[] arr, ` ` ` `int` `low, ` `int` `high) ` ` ` `{ ` ` ` `int` `mid = 0; ` ` ` `while` `(low <= high) { ` ` ` `mid = (low + high) / 2; ` ` ` ` ` `// if true, then 'mid' is the index of ` ` ` `// first '1' ` ` ` `if` `(arr[mid] == 1 && (mid == 0 || arr[mid - 1] == 0)) ` ` ` `break` `; ` ` ` ` ` `// first '1' lies to the left of 'mid' ` ` ` `else` `if` `(arr[mid] == 1) ` ` ` `high = mid - 1; ` ` ` ` ` `// first '1' lies to the right of 'mid' ` ` ` `else` ` ` `low = mid + 1; ` ` ` `} ` ` ` ` ` `// required index ` ` ` `return` `mid; ` ` ` `} ` ` ` ` ` `// function to find the index of first 1 in ` ` ` `// an infinite sorted array of 0's and 1's ` ` ` `public` `static` `int` `posOfFirstOne(` `int` `[] arr) ` ` ` `{ ` ` ` `// find the upper and lower bounds ` ` ` `// between which the first '1' would ` ` ` `// be present ` ` ` `int` `l = 0, h = 1; ` ` ` ` ` `// as the array is being considered ` ` ` `// infinite therefore 'h' index will ` ` ` `// always exist in the array ` ` ` `while` `(arr[h] == 0) { ` ` ` ` ` `// lower bound ` ` ` `l = h; ` ` ` ` ` `// upper bound ` ` ` `h = 2 * h; ` ` ` `} ` ` ` ` ` `// required index of first '1' ` ` ` `return` `indexOfFirstOne(arr, l, h); ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr = { 0, 0, 1, 1, 1, 1 }; ` ` ` `Console.Write(` `"Index = "` `+ posOfFirstOne(arr)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to find ` `// the index of first 1 in an ` `// infinite sorted array of ` `// 0's and 1's ` ` ` `// function to find the index ` `// of first '1' binary search ` `// technique is applied ` `function` `indexOfFirstOne(` `$arr` `, ` `$low` `, ` `$high` `) ` `{ ` ` ` `$mid` `; ` ` ` `while` `(` `$low` `<= ` `$high` `) ` ` ` `{ ` ` ` `$mid` `= (` `$low` `+ ` `$high` `) / 2; ` ` ` ` ` `// if true, then 'mid' is ` ` ` `// the index of first '1' ` ` ` `if` `(` `$arr` `[` `$mid` `] == 1 ` `and` ` ` `(` `$mid` `== 0 ` `or` `$arr` `[` `$mid` `- 1] == 0)) ` ` ` `break` `; ` ` ` ` ` `// first '1' lies to ` ` ` `// the left of 'mid' ` ` ` `else` `if` `(` `$arr` `[` `$mid` `] == 1) ` ` ` `$high` `= ` `$mid` `- 1; ` ` ` ` ` `// first '1' lies to ` ` ` `// the right of 'mid' ` ` ` `else` ` ` `$low` `= ` `$mid` `+ 1; ` ` ` `} ` ` ` ` ` `// required index ` ` ` `return` `ceil` `(` `$mid` `); ` `} ` ` ` `// function to find the index of ` `// first 1 in an infinite sorted ` `// array of 0's and 1's ` `function` `posOfFirstOne(` `$arr` `) ` `{ ` ` ` `// find the upper and lower ` ` ` `// bounds between which the ` ` ` `// first '1' would be present ` ` ` `$l` `= 0; ` `$h` `= 1; ` ` ` ` ` `// as the array is being considered ` ` ` `// infinite therefore 'h' index will ` ` ` `// always exist in the array ` ` ` `while` `(` `$arr` `[` `$h` `] == 0) ` ` ` `{ ` ` ` ` ` `// lower bound ` ` ` `$l` `= ` `$h` `; ` ` ` ` ` `// upper bound ` ` ` `$h` `= 2 * ` `$h` `; ` ` ` `} ` ` ` ` ` `// required index ` ` ` `// of first '1' ` ` ` `return` `indexOfFirstOne(` `$arr` `, ` ` ` `$l` `, ` `$h` `); ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `(0, 0, 1, 1, 1, 1); ` `echo` `"Index = "` `, ` ` ` `posOfFirstOne(` `$arr` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Index = 2

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

This article is contributed by **Ayush Jauhari**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the index of first 1 in a sorted array of 0's and 1's
- Find index of first occurrence when an unsorted array is sorted
- Find the count of unvisited indices in an infinite array
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Find index of an extra element present in one sorted array
- Find first and last positions of an element in a sorted array
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Shortest distance between two nodes in an infinite binary tree
- Find H-Index for sorted citations using Binary Search
- Queries for number of distinct elements from a given index till last index in an array
- Sort a nearly sorted (or K sorted) array
- Even numbers at even index and odd numbers at odd index
- Number of permutations such that sum of elements at odd index and even index are equal
- Print all elements in sorted order from row and column wise sorted matrix
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- First strictly greater element in a sorted array in Java
- First strictly smaller element in a sorted array in Java
- Check if every index i has an index j such that sum of elements in both directions are equal
- Count of contiguous subarrays possible for every index by including the element at that index