-
Notifications
You must be signed in to change notification settings - Fork 40
/
count_strictly_increasing_subarrays.c
89 lines (84 loc) · 1.93 KB
/
count_strictly_increasing_subarrays.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Date: 2018-10-21
*
* Description:
* Given an array of integers, count number of subarrays (of size more than one)
* that are strictly increasing.
* Example:
* Input: arr[] = {1, 4, 3}
* Output: 1, There is only one subarray {1, 4}
*
* Input: arr[] = {1, 2, 2, 4}
* Output: 2, There are 2 subarrays {1, 2} and {2, 4}
*
* Approach:
* Uses maths formula, if there are N increasing numbers we can have N*(N-1)/2
* total sets possible.
*
* So, scan array from starting and increment a temp counter as elements are in
* increasing order. As element decrements update total-sets with
* temp*(temp-1)/2
*
* Don't forget to update total-sets outside loop, this will take care of last
* increasing subarray.
*
* Complexity:
* O(N)
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0;
int n = 0;
int *A = NULL;
int tmp_len = 1;
int total_sets = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
A = (int *)malloc(sizeof(int)*n);
for (i = 0; i < n; i++) {
printf("Enter element[%d]: ", i);
scanf("%d", &A[i]);
}
for (i = 0; i < (n - 1); i++) {
if (A[i] < A[i + 1])
tmp_len++;
else {
total_sets += (tmp_len * (tmp_len - 1))/2;
tmp_len = 1;
}
}
total_sets += (tmp_len * (tmp_len - 1))/2; // Take care of last incr numbers
printf("Total sets: %d\n", total_sets);
return 0;
}
/*
* Output:
* -------------------
* Enter number of elements: 3
* Enter element[0]: 1
* Enter element[1]: 4
* Enter element[2]: 3
* Total sets: 1
*
* Enter number of elements: 4
* Enter element[0]: 1
* Enter element[1]: 2
* Enter element[2]: 3
* Enter element[3]: 4
* Total sets: 6
*
* Enter number of elements: 4
* Enter element[0]: 1
* Enter element[1]: 2
* Enter element[2]: 2
* Enter element[3]: 4
* Total sets: 2
*
* Enter number of elements: 4
* Enter element[0]: 5
* Enter element[1]: 4
* Enter element[2]: 3
* Enter element[3]: 2
* Total sets: 0
*/