-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecord_breaker.cpp
72 lines (60 loc) · 1.39 KB
/
record_breaker.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
71
72
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the length of the array: " << endl;
cin >> n;
// My approach
// int a[n], l[n];
// for (int i = 0; i < n; i++)
// {
// cout << "Enter the element at the position " << i << " in the array: " << endl;
// cin >> a[i];
// l[i] = 0;
// }
// int mx = a[0];
// if (a[0] > a[1])
// {
// l[0] = a[0];
// }
// for (int i = 1; i < n; i++)
// {
// mx = max(mx, a[i - 1]);
// if (a[i] > mx && a[i] > a[i + 1])
// {
// l[i] = a[i];
// }
// }
// for (int i = 0; i < n; i++)
// {
// if (l[i] != 0)
// {
// cout << l[i] << " ";
// }
// }
// =============================================================================
// Optimized Approach
if (n == 1)
{
cout << "1" << endl;
return 0;
}
int a[n];
for (int i = 0; i < n; i++)
{
cout << "Enter the element at the position " << i << " in the array: " << endl;
cin >> a[i];
}
int mx = -1;
int ans = 0;
for (int i = 0; i < n; i++)
{
if (a[i] > mx && a[i] > a[i + 1])
{
ans++;
}
mx = max(mx, a[i]);
}
cout << "The total no. of record breaking days in the given array are: " << ans << endl;
}