You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Codechef Problems/Red_alert.c
+19-10Lines changed: 19 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -61,34 +61,43 @@ int main(void) {
61
61
intt,n,d,h;
62
62
63
63
scanf("%d",&t);
64
+
// t represents the number of test cases
64
65
65
-
while(t--)
66
+
while(t--)//the loop runs for t times
66
67
{
67
68
scanf("%d %d %d",&n,&d,&h);
69
+
// n, d and h respectively stores the input values
68
70
intsum=0;intval; intret=0;
69
-
for(inti=0;i<n;i++)
71
+
for(inti=0;i<n;i++)// this loop take n values as user input
70
72
{
71
-
scanf("%d",&val);
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.
72
76
if(val>0)
73
77
{
74
-
sum+=val;
78
+
//if val( the level of water on ith day > 0
79
+
sum+=val; // total level = previous(stored in sum itsef) + val;
75
80
76
81
}
77
82
if(val==0)
78
83
{
84
+
//if val( the level of water on ith day = 0
79
85
sum=(sum<d)?0 : (sum-d);
80
-
86
+
// the ternary operator above checks if sum<d for true: sum =0 for false: sum-d
81
87
}
82
-
if(sum>h)
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
83
90
{
84
-
ret=1;
91
+
ret=1; //if red alert i.e. sum > h , the variable ret is initialised with value 1.
0 commit comments