Skip to content

Commit 4c358cb

Browse files
authored
Merge pull request #852 from simranquirky/master
Red alert solution (codechef problem) in C language.
2 parents 86a2f60 + 3b94f77 commit 4c358cb

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

Codechef Problems/Red_alert.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* PROBLEM STATEMENT
2+
3+
4+
Finally a monsoon has come. According to the Meteorological Department, there will be rain in the upcoming N days in the city. Initially, the water level of the city is zero millimetres. The amount of rain on the i-th day can be described by an integer Ai as follows:
5+
6+
If Ai>0, the water level of the city increases by Ai millimetres on the i-th day.
7+
If Ai=0, there is no rain on the i-th day. The water level of the city decreases by D millimetres on such a day. However, if the water level is less than D millimetres before the i-th day, then it becomes zero instead.
8+
There will be a red alert in the city if the water level becomes strictly greater than H millimetres on at least one of the N days. Determine if there will be a red alert.
9+
10+
Input Format
11+
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
12+
The first line of each test case contains three space-separated integers N, D and H.
13+
The second line contains N space-separated integers A1,A2,…,AN.
14+
Output Format
15+
For each test case, print a single line containing the string "YES" if there will be a red alert or "NO" otherwise.
16+
17+
Constraints
18+
1≤T≤103
19+
1≤N,D≤102
20+
0≤Ai≤102 for each valid i
21+
1≤H≤104
22+
Subtasks
23+
Subtask #1 (100 points): original constraints
24+
25+
Sample Input 1
26+
4
27+
4 2 6
28+
1 3 0 2
29+
2 1 100
30+
1 100
31+
4 2 3
32+
1 2 0 2
33+
3 7 10
34+
5 3 9
35+
Sample Output 1
36+
NO
37+
YES
38+
NO
39+
YES
40+
Explanation
41+
Example case 1:
42+
43+
On the first day, the water level of the city increases to 1 millimtre.
44+
On the second day, the water level increases by 3 millimeters and becomes 1+3=4 millimtres.
45+
On the third day, there is no rain in the city, so the water level decreases by D=2 millimtres and becomes 4−2=2 millimtres.
46+
On the fourth day, the water level increases by 2 millimtres and becomes 2+2=4 millimtres.
47+
There will be no red alert in the city because the water level does not exceed H=6 millimtres on any of the four days.
48+
49+
Example case 2: The water level of the city on the 2-nd day is equal to 101 millimtres, which is greater than H=100 millimtres, so there will be a red alert in the city.
50+
51+
Example case 3: The water levels of the city on the four days are [1,3,1,3]. The water level is equal to H=3 millimtres on the second and fourth day, but it does not exceed the threshold.
52+
53+
Example case 4: There will be a red alert in the city on the 3-rd day. */
54+
55+
// SOLUTION
56+
57+
#include <stdio.h>
58+
59+
int main(void) {
60+
61+
int t,n,d,h;
62+
63+
scanf("%d",&t);
64+
// t represents the number of test cases
65+
66+
while(t--) //the loop runs for t times
67+
{
68+
scanf("%d %d %d",&n,&d,&h);
69+
// n, d and h respectively stores the input values
70+
int sum=0;int val; int ret=0;
71+
for(int i=0;i<n;i++) // this loop take n values as user input
72+
{
73+
74+
scanf("%d",&val); // the values inputted are stored each time in the variable val
75+
//note: here a single variable is used instead of an array beacuse the operations are done on the variable here itself i.e. within this loop.
76+
if(val>0)
77+
{
78+
//if val( the level of water on ith day > 0
79+
sum+=val; // total level = previous(stored in sum itsef) + val;
80+
81+
}
82+
if(val==0)
83+
{
84+
//if val( the level of water on ith day = 0
85+
sum=(sum<d)?0 : (sum-d);
86+
// the ternary operator above checks if sum<d for true: sum =0 for false: sum-d
87+
}
88+
89+
if(sum>h) //checks the level each day (level is calculated by carrying out arithmetic operations on variable val which is finally stored in variable sum
90+
{
91+
ret=1; //if red alert i.e. sum > h , the variable ret is initialised with value 1.
92+
break;
93+
}
94+
}
95+
if(ret==0)
96+
printf("NO\n");
97+
else
98+
printf("YES\n");
99+
// Time Complexity : O(N)
100+
// Space Complexity: 0(1)
101+
102+
}
103+
104+
105+
return 0;
106+
}

0 commit comments

Comments
 (0)