-
Notifications
You must be signed in to change notification settings - Fork 1
/
Transition.cpp
48 lines (43 loc) · 862 Bytes
/
Transition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
int transitionPoint(int arr[], int n);
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int a[n], i;
for (i = 0; i < n; i++) {
cin >> a[i];
}
cout << transitionPoint(a, n) << endl;
}
return 0;
}
// } Driver Code Ends
int transitionPoint(int arr[], int n) {
// code here
if(arr[n-1] == 0)
return -1;
if(n==1)
{
if(arr[0] == 1)
return 0;
else
return -1;
}
int L = 0;
int R = n-1;
while(L <= R)
{
int mid = floor((L+R) /2);
if(arr[mid] == 1 && arr[mid-1] != 1)
return mid;
else if(arr[mid] == 0)
L = mid+1;
else if(arr[mid] == 1)
R = mid-1;
}
return -1;
}