We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

vector<string>cavityMap(vector<string>arr){intn=arr.size();for(inti=1;i<n-1;i++)//ignoring first and last rows{for(intj=1;j<n-1;j++)//ignoring first and last columns{if(arr[i][j]-48>arr[i-1][j]-48&&arr[i][j]-48>arr[i+1][j]-48&&arr[i][j]-48>arr[i][j-1]-48&&arr[i][j]-48>arr[i][j+1]-48)arr[i][j]='X';}}returnarr;}

https://stackoverflow.com/questions/439573/how-to-convert-a-single-char-into-an-int There is a comment in top answer that states that -48 woun't work on every encoding. There is another aspect - your question. Would you have a question if the code was: arr[i][j] - '0' ...? Probably not. In my opinion the use of -48 falls into bad practice. And note: because both operands are subtracted the subtraction itself is obsolete.

he has taken the array as string and so for comparing numbers he subtracted the array with ascii value of number '0' i.e. 48 which will give the number directly during comparison.

## Cavity Map

You are viewing a single comment's thread. Return to all comments →

you cant replace it during the check.

not need to put 48 in comparison

Why u substracted with 48???

https://stackoverflow.com/questions/439573/how-to-convert-a-single-char-into-an-int There is a comment in top answer that states that -48 woun't work on every encoding. There is another aspect - your question. Would you have a question if the code was: arr[i][j] - '0' ...? Probably not. In my opinion the use of -48 falls into bad practice. And note: because both operands are subtracted the subtraction itself is obsolete.

he has taken the array as string and so for comparing numbers he subtracted the array with ascii value of number '0' i.e. 48 which will give the number directly during comparison.