Search in Infinite Sorted 0-1 Array Problem Statement
You are provided with an infinite sorted array that consists exclusively of 0s followed by 1s. Your task is to determine the index of the first occurrence of the number 1 in this array.
Example:
Input:
Array = [0, 0, 0, 0, 1, 1, 1, 1, ...]
Output:
4
Explanation:
In the given array, the first occurrence of '1' is at index 4. Therefore, the answer is 4.
Constraints:
- 0 <= ARR[i] <= 1
- Time limit: 1 second
Note:
The array is infinite, hence you won't receive the actual array. Instead, you can access its elements using a function called get(i)
, which returns the value at index i
.
Note that the provided input does not directly represent the infinite array. Instead, it indicates the index of the first occurrence of 1, which remains hidden. Your implementation should ensure that the answer fits into a 64-bit integer.
Input:
The only input line contains an integer X, the index of the first occurrence of 1. (Hidden to the user)
Output:
Print an integer denoting the index of the first occurrence of 1.
Find the index of the first occurrence of 1 in an infinite sorted 0-1 array.
Use binary search to efficiently find the index of the first occurrence of 1.
Keep doubling the search range until you find a...read more
Top Procol Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Reviews
Interviews
Salaries
Users/Month