Piyush and magical park

Please refer lines 9-12 in the code

When the strength s<k we break the inner loop and go to the next row but even at the starting of the next row the strength would be s<k as the “IF” condition is applied in the starting of the loop. So according to this logic we will come out of both the FOR loops and print NO and not traverse any next rows.

hi @ujj1804_1156aee80205d0bf
so basically when s < k, u break from that inner for loop… u go to next for loop and every time due to break, we come out… so what u can do is after inner loop u place another if condition to check if s < k, then break so that we come out of outer for loop also…

But if we put another ‘if’ condition s<k after the inner loop , it won’t traverse any rows and print NO , what if by traversing the next rows s would become grater than k , but according to our ‘if’ condition it will print NO which will be the wrong output.

My main concern is that once the variable ‘success’ becomes false it won’t traverse any other rows even if the value of s could become greater than k by traversing these rows ,and give the output NO .

hi @ujj1804_1156aee80205d0bf
can u elaborate using ur code what concern u are facing, since I can see u have already successfully submitted the code…

I just have some doubt in the logic of the code.

Please refer to the code below , my doubt is that suppose while traversing the first row(iteration i=1)
‘s’ becomes less than ‘k’ thus the if(s<k) condition will be true and the control would break from the inner loop and thus i will become 2. Now when i=2 still in the memory s<k is true and thus when
The if(s<k) condition is encountered again the inner loop would break again and this will keep on happening for the remaining values of i without even traversing all the rows which might have made s>k . This will ulimately give us the output NO

#include

using namespace std;

void magical_park(char a[][100],int m,int n,int k,int s)

{

 bool success=true;

 for(int i=0;i<n;i++){

     for(int j=0;j<m;j++){

         char ch=a[i][j];

         if(s<k){

             success=false;

             break;

         }

         if(ch=='*'){

             s=s+5;

         }

         else if(ch=='.'){

             s=s-2;

         }

         else {

             break;

         }

         if(j!=n-1){

             s--;

         }

     }

 }

 if(success==true){

     cout<<"Yes"<<endl;

     cout<<s;

 }

 else{

     cout<<"No"<<endl;

 }

}

int main(){

 int m,n,k,s;

 cin>>m>>n>>k>>s;

 char park[100][100];

 for(int i=0;i<m;i++){

     for(int j=0;j<n;j++){

         cin>>park[i][j];

     }

 }

  magical_park(park,m,n,k,s);

  return 0;

}

hi @ujj1804_1156aee80205d0bf
so as and when if(s<k) condition becomes true, it will not enter again as now it won’t be able to go ahead… so there is no point that value may increase later on… once it is less than k u cant go ahead…

1 Like

Ok sir thank u very much , so when s<k actually we should directly come out of both loops which would reduce the number of iterations rather than coming out of inner loop only.

Yes @ujj1804_1156aee80205d0bf

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.