P20764 [GeeksforGeeks] Index of first 1 in a sorted array of 0s and 1s4048
普及-
题目描述

Given a sorted array consisting 0s and 1s. The task is to find the index of first 1 in the given array.

Example 1:
Input :
arr[] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1}
Output :
6
Explanation:
The index of first 1 in the array is 6.
Example 2:
Input :
arr[] = {0, 0, 0, 0}
Output :
-1
Explanation:
1's are not present in the array.

Your Task:
You don't need to read input or print anything. Your task is to complete the function firstIndex() which takes the array A[] and its size N as inputs and returns the index of first 1. If 1 is not present in the array then return -1.
Expected Time Complexity: O(Log (N))
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 10^{6}
0 ≤ A_{i} ≤ 1

输入格式

见题面

输出格式

见题面

样例输入
arr[] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1}
样例输出
6