Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Find Transition Point

Easy Acceptance 37.9% Points 20

Given a sorted array, arr[] containing only 0s and 1s, find the transition point, i.e., the first index where 1 was observed, and before that, only 0 was observed. If arr does not have any 1, return -1. If array does not have any 0, return 0.

Examples
Example 1

Example 1

Input: arr[] = [0, 0, 0, 1, 1]

Output: 3

Explanation: index 3 is the transition point where 1 begins.

Example 2

Example 2

Input: arr[] = [0, 0, 0, 0]

Output: -1

Explanation: Since, there is no "1", the answer is -1.

Constraints
  • 1 <= arr.size() <= 10^5
  • 0 <= arr[i] <= 1
Hints
  • Expected Time Complexity: O(log n)
  • Expected Auxiliary Space: O(1)
Topics
Array Searching Algorithms
Companies
Amazon Paypal
Added 11 Apr 2025 37.9% acceptance
Ln 1, Col 1Sign in to save your code
0 0 0 1 1
3