Skip to content

Commit 3b94f77

Browse files
authored
Update Red_alert.c
Comments and complexity added
1 parent 0e32461 commit 3b94f77

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

Codechef Problems/Red_alert.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,43 @@ int main(void) {
6161
int t,n,d,h;
6262

6363
scanf("%d",&t);
64+
// t represents the number of test cases
6465

65-
while(t--)
66+
while(t--) //the loop runs for t times
6667
{
6768
scanf("%d %d %d",&n,&d,&h);
69+
// n, d and h respectively stores the input values
6870
int sum=0;int val; int ret=0;
69-
for(int i=0;i<n;i++)
71+
for(int i=0;i<n;i++) // this loop take n values as user input
7072
{
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.
7276
if(val>0)
7377
{
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;
7580

7681
}
7782
if(val==0)
7883
{
84+
//if val( the level of water on ith day = 0
7985
sum=(sum<d)?0 : (sum-d);
80-
86+
// the ternary operator above checks if sum<d for true: sum =0 for false: sum-d
8187
}
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
8390
{
84-
ret=1;
91+
ret=1; //if red alert i.e. sum > h , the variable ret is initialised with value 1.
92+
break;
8593
}
8694
}
87-
if(ret==0)
95+
if(ret==0)
8896
printf("NO\n");
89-
else
97+
else
9098
printf("YES\n");
91-
99+
// Time Complexity : O(N)
100+
// Space Complexity: 0(1)
92101

93102
}
94103

0 commit comments

Comments
 (0)