-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrictly_inc_subarray.cpp
More file actions
44 lines (37 loc) · 916 Bytes
/
strictly_inc_subarray.cpp
File metadata and controls
44 lines (37 loc) · 916 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
// increasing subarrays in O(n) time.
#include<bits/stdc++.h>
using namespace std;
int countIncreasing(int arr[], int n)
{
int cnt = 0; // Initialize result
// Initialize length of current increasing
// subarray
int len = 1;
// Traverse through the array
for (int i=0; i < n-1; ++i)
{
// If arr[i+1] is greater than arr[i],
// then increment length
if (arr[i + 1] > arr[i])
len++;
// Else Update count and reset length
else
{
cnt += (((len - 1) * len) / 2);
len = 1;
}
}
// If last length is more than 1
if (len > 1)
cnt += (((len - 1) * len) / 2);
return cnt;
}
// Driver program
int main()
{
int arr[] = {1, 2, 2, 4};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Count of strictly increasing subarrays is "
<< countIncreasing(arr, n);
return 0;
}