Funky problem solution


getting test cases wrong
is my code correct for answer

Hi @Faizan-Ali-1395131367301898

Here is your corrected code :


I have changed few things and mentioned their reason as comment.

can u please explain how the code is runninh

First of all you are computing number of valid cell in which knight can move and storing it in sum when arr[i][j] is 1. Then call is made to the function where we compute hi which stores the maximum of value of visited squares and hi is taken as max of hi and count which is incremented every time the 8 more fun call are made according to the moves possible in the ques. Then we at the end hi as total number of cell that can be visited. So total number of cells that can not be reached is sum(total valid cells) - hi(total cell visited).

can u please explain clearly

Which segment of the code you are unable to understand ?

i cannot understand algorithm

It is basically a backtracking algorithm in which at each cell we try to visit other 8 combinations of next move possible and there we check if this cell exits or not and finally we get our result. To understand this problem more accurately try to watch video lectures related to backing tracking algorithm then you will easily be able to solve this ques on your own.

i don’t understand why cont and hi ar taken

hi is a global variable which stores the maximum number of cells that can be visited while count is a local variable of that function. To explain their importance consider that you have make call from main function to fun with count as 0 and hi is computed as max(0,0). When fun first recursive call is made to fun with count as 1 and in that call hi becomes 1 and this how at the end hi attain a value equal to count. But when that call backtracks to initial fun call then there we see that count=0 and hi is value>0. Then second fun call comes into consideration where count goes again start from one and there if hi < count then we again updates the value of hi. This is how hi and count have importance of their own.