Shortest Path Reduced String Problem

I have written the code and it only prints N and E directions.

https://ide.geeksforgeeks.org/MWc78rcCy1

In your code at line 31 you have
for(int i=0; i<x; i++){
if(x>0){
cout<<β€œE”;
}
else if(x<0){
cout<<β€œW”;
}
}
take a look at the condition of for loop ,the loop will run if i<x and you initialized i with 0 ,so when x <0 then i>x and loop won’t execute thus not printing W and similar argument is for case of not printing S

I have corrected your code : https://ide.codingblocks.com/s/182487

1 Like

Thanks a lot abhijeet!

In case of any doubt feel free to ask me :slight_smile:
If you got the solution mark your doubt as resolved

What is the logic behind x++ after cout<<β€œW”; and vice versa?

let say if x=-3 that means that we are 3 units to the left of origin so we have to print W three times
while(x!=0){
cout<<W;
x++;
}
ensures that loop runs 3 times and x will take values of 3 2 1 0(loop breaks)

1 Like