[Java] Beginner Friendly Detailed Explanation and Understandable Recursive Approach for Beginners
So to begin with, let\'s divide question into three scenarios:\n\n1. Ball falls\n2. Ball gets stuck\n3. Ball moves through array\n\nScenario 1\nFirst lets see w
So to begin with, let\'s divide question into three scenarios:\n\n1. Ball falls\n2. Ball gets stuck\n3. Ball moves through array\n\n**Scenario 1**\nFirst lets see when will will the 1. scenario work, i guess if the ball has reached the end most row, that means it has fallen sucessfully, so, this is our only base case:\n```\nif(i==m){\n           //assign the corresponding coloumn to my answer array\n\t\t   ans[k]=j;\n\t\t   return;\n        }\n```\n\n**Scenario 2**\nNow, lets come to 2. scenario, if you think carefully, you would get to learn that ball gets stuck only when:\n\n1. Value of arr[i][j]=1 and Right neighbour is wall or Right neighbour is -1 i.e.\n\t\t1->wall OR 1->-1\n\t\t\n\t\t```\n\t\tif(arr[i][j]==1 && (j+1>=n || arr[i][j+1]==-1))\n            return;\n\t\t```\n\n2. Value of arr[i][j]=-1 and Left neighbour is wall or Left neighbour is 1 i.e.\n\twall<- -1 OR 1<- -1\n\t```\n\telse if(arr[i][j]==-1 && (j-1<0 || arr[i][j-1]==1))\n            return;\n\t```\n\t\t\n**Scenario 3**\nLastly, ball will move through the array if it escapes the above two scenarios, so it can move in two ways:\n1. Diagonal Right: when arr[i][j]=1\n```\n else if(arr[i][j]==1)\n            func(arr,i+1,j+1,m,n,ans);\n```\n\n2. Diagonal Left: when arr[i][j]=-1\n```\nelse if(arr[i][j]==-1)\n            func(arr, i+1, j-1,m,n, ans);\n```\n\n**CODE**\n\n```\nclass Solution {\n    int k=0;\n    public int[] findBall(int[][] arr) {\n        \n        int m=arr.length;\n        int n=arr[0].length;\n\t\t\n        //Initialize []ans with -1\n        int []ans=new int[n];\n        Arrays.fill(ans, -1);                  \n        \n\t\t//Step-by-Step iterate through first row of [][]arr\n        for(int j=0;j<n;j++){\n            func(arr,0,j,m,n,ans);\n            k++;\n        }\n        \n        return ans;\n    }\n    \n    void func(int [][]arr, int i, int j, int m, int n, int []ans){\n        \n\t\t// if i has traversed through last row, that means ball has fallen successfully, hence save ans[k]=jth index of arr[i]\n        if(i==m){\n            ans[k]=j;\n            return;\n        }\n\t\t\n        //if 1 is being traversed and its RIGHT neighbour is wall or RIGHT neighbour is -1\n\t\t//ball will get stuck\n        if(arr[i][j]==1 && (j+1>=n || arr[i][j+1]==-1))\n            return;\n\t\t\t\n        //if -1 is being traversed and its LEFT neighbour is wall or LEFT neighbour is 1\n\t\t//ball will get stuck\n        else if(arr[i][j]==-1 && (j-1<0 || arr[i][j-1]==1))\n            return;\n        \n\t\t// if 1 is being traversed so take me to RIGHT diagonal because 1->\\\n        else if(arr[i][j]==1)\n            func(arr,i+1,j+1,m,n,ans);\n        \n\t\t// if -1 is being traversed so take me to LEFT diagonal because -1->/\n        else if(arr[i][j]==-1)\n            func(arr, i+1, j-1,m,n, ans);\n        \n    }\n}\n```\n\nHope it helps! :D
['Dynamic Programming', 'Backtracking', 'Depth-First Search', 'Recursion', 'Java']