Given a sorted array of size N, consisting of only 0’s and 1’s. The problem is to find the position of first ‘1’ in the sorted array Assuming 1 based indexing.
It could be possible that the array consists of only 0’s or only 1’s. If 1’s are not present in the array then print “-1”.
The first line contains an Integer 't' which denotes the number of test cases/queries to be run.
Then the test cases follow.
The first line of input for each test case/query contains an integer N representing the size of the array.
The second line of input for each test case/query contains N space-separated integers consisting of only ‘0’s and ‘1’s in the sorted order.
Output Format:
For each test case, print the position of the first ‘1’ in the sorted array. If 1’s are not present in the array then print “-1”.
Output for every test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the function.
1 <= t <= 100
0 <= N <= 10^4
Time Limit: 1sec
3
5
0 0 0 1 1
4
1 1 1 1
3
0 0 0
4
1
-1
Test Case 1:
Three 0’s are present at the beginning and first ‘1’ is present at the 4th position.
Test Case 2:
First ‘1’ is present at the 1st position.
Test Case 3:
1’s are not present in the array so we print -1.
Try to find the position by traversing the whole array.
O(N), where N is the number of elements in the array.
Since we are traversing the array once.
O(1)
Since we are using constant extra space.