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.
AnswerBot
2d

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

Help your peers!
Add answer anonymously...
Procol Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter