467. Find the Transition Point in 0s and 1s Array

EasyBinary SearchArrayBinary Search

Given a sorted binary array arr with all 0s followed by all 1s, find the index of the first 1 (the transition point). Return -1 if no 1 exists. Solve in O(log n).

Input: A sorted binary array of 0s and 1s.

Output: Index of the first 1, or -1 if all zeros.

Examples

Example 1
Input: [0,0,0,1,1,1]
Output: 3
Explanation: First 1 is at index 3.
Example 2
Input: [1,1,1,1]
Output: 0
Explanation: All 1s; transition point is index 0.
Example 3
Input: [0,0,0,0]
Output: -1
Explanation: No 1s present; return -1.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →