Difficulty Level : Easy
Submissions : 354
Asked In :
Marks :10
: 4 | : 0
You are given a sorted (in ascending order) binary array (consisting only of 0's and 1's). Print the index(0-based) of the first 1 and last 0 in the array. If no 0's exist, print -1 instead of the index of the last 0. If no 1's exist, print N instead of the index of the first 1.
Only attempt to solve this in logN complexity.
The first line of input contains N - the size of the array. The second line of input contains N integers - that can be either 0 or 1. The N integers are guaranteed to be sorted in ascending order.
1 <= N <= 100000
The output should consist of two space separated integers in a line. First integer should be 0-based index of first 1 in the array. If no 1's exist, print N instead of the index of the first 1. Second integer should be 0-based index of last 0 in the array. If no 0's exist, print -1 instead of the index of the last 0.
5 0 0 0 1 1
3 2
5 0 0 0 0 0
5 4
5 1 1 1 1 1
0 -1
5 0 1 1 1 1
1 0
5 0 0 0 0 1
4 3
5 0 0 1 1 1
2 1
4 0 0 0 0
4 3
4 0 0 0 1
3 2
4 0 0 1 1
2 1
4 0 1 1 1
1 0
4 1 1 1 1
0 -1
1 1
0 -1
1 0
1 0
You need to login to view your submissions.
You need to login to view all submissions.
Result : Executed
Feel something is wrong with the test cases?
Result : Accepted
Test Cases :
But to Run or Submit the Problem, you need to Log In.
Continue to Log InYour challenge has been submitted successfully.
You will get a response soon via WhatsApp or Email.
Do let us know your issue.