-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem06.c
More file actions
63 lines (53 loc) Β· 1.26 KB
/
Copy pathproblem06.c
File metadata and controls
63 lines (53 loc) Β· 1.26 KB
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
#include <stdio.h>
#include <stdlib.h>
void getTemperature(int n, int *temp)
{
printf("Enter the temperature for each day(-1000 - 1000): ");
for (int i = 0; i < n; i++)
{
scanf("%d", (temp + i));
if (temp[i] > 1000 || temp[i] < -1000)
{
printf("!! Range Exceed !!\n");
i--;
}
}
return;
}
int findLongSubarray(int n, int *temp)
{
int currLen = 1, longLen = 1;
for (int i = 1; i < n; i++)
{
if (temp[i] > temp[i - 1])
currLen++;
// it can be done using nested if else condition
else if (currLen > longLen)
{
longLen = currLen;
currLen = 1;
}
else
currLen = 1;
}
if (currLen > longLen)
longLen = currLen;
return longLen;
}
int main()
{
int n;
printf("Enter the number of days: ");
scanf("%d", &n);
int *temperature = (int *)malloc(n * sizeof(int));
if (temperature == NULL)
{
printf("Memory Allocation Failed\n");
return 1;
}
getTemperature(n, temperature);
int longSubarray = findLongSubarray(n, temperature);
printf("Length of longest strictly increasing subarray = %d", longSubarray);
free(temperature);
return 0;
}