-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Find_transition_point.cpp
71 lines (64 loc) · 1.23 KB
/
Find_transition_point.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Description : Given a sorted array containing only 0s and 1s, find the transition point.
*/
#include <bits/stdc++.h>
using namespace std;
int transitionPoint(int arr[], int n)
{
// code here
if (arr[0])
return 0;
//using modified binary search
//lb= lower bound
//ub= upper bound
int lb = 0;
int ub = n - 1;
while (lb <= ub)
{
int mid = (lb + ub) / 2;
if (arr[mid] == 0)
{
lb = mid + 1;
}
else if (arr[mid] == 1)
{
if (arr[mid - 1] == 0)
return mid;
else
ub = mid - 1;
}
}
return -1;
}
int main()
{
//size of an array
int n;
cout << "Enter the size of an array : " << endl;
cin >> n;
int a[n];
cout << "Enter the data in the array : " << endl;
//taking input in array
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "Transition point in array : " << endl;
cout << transitionPoint(a, n) << endl;
return 0;
}
/*
Time complexity : O(log n)
Space complexity : O(n)
*/
/*
Test Case :
Input :
Enter the size of an array :
5
Enter the data in the array :
0 0 0 1 1
Output :
Transition point in array :
3
*/