-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestBitonicSubarray.cpp
More file actions
64 lines (52 loc) · 984 Bytes
/
LongestBitonicSubarray.cpp
File metadata and controls
64 lines (52 loc) · 984 Bytes
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
#include <iostream>
#include <vector>
// QUESTION: https://www.techiedelight.com/find-longest-bitonic-subarray-array/
class Bitonic
{
private:
const std::vector<int>& arr;
public:
Bitonic(const std::vector<int>& input)
: arr(input)
{
}
void find()
{
int max_len = 0;
int start_indx = 0;
int end_indx = 0;
int j = 0;
while (j + 1 < arr.size())
{
int len = 1;
while (j + 1 < arr.size() && arr[j] < arr[j + 1])
{
j++;
len++;
}
while (j + 1 < arr.size() && arr[j] > arr[j + 1])
{
j++;
len++;
}
if (len > max_len)
{
max_len = len;
start_indx = j - len + 1;
end_indx = j;
}
}
std::cout << "Length of longest bitonic subarray is " << max_len << " and it is:" << std::endl;
for (int i = start_indx; i <= end_indx; ++i)
{
std::cout << arr[i] << std::endl;
}
}
};
int main()
{
std::vector<int> arr{ 3, 5, 8, 4, 5, 9, 10, 8, 5, 3, 4 };
Bitonic obj(arr);
obj.find();
return 0;
}