[1706] Where Will the Ball Fall,Recursive Solution in C++,Fully Explained With Comments
```\nclass Solution {\npublic:\n    int fB(int i,int j,vector> &grid)   //Helper function to find where each ball ends its journey\n    {\n        if(i==grid.si
```\nclass Solution {\npublic:\n    int fB(int i,int j,vector<vector<int>> &grid)   //Helper function to find where each ball ends its journey\n    {\n        if(i==grid.size()-1)                       //if the ball is at (n-1)th row \n        {\n            if(j<grid[0].size()-1&&grid[i][j]==1&&grid[i][j+1]!=-1)        //Checking condition can ball collide with a wall?\n                return j+1;                                                                //Also checking for v-shape formation\n            else if(j>0&&grid[i][j]==-1&&grid[i][j-1]!=1)                      //Same condition if grid[i][j]=-1\n                return j-1;                        \n            else\n                return -1;\n        }\n        if(i<0||i>=grid.size()||j<0||j>=grid[0].size())                           //if ball collides with walls on the left or right side\n            return -1;\n        if(j+1<grid[0].size()&&grid[i][j]==1&&grid[i][j+1]==-1)     //Checking v-shape formation\n            return -1;\n        if(j>0&&grid[i][j]==-1&&grid[i][j-1]==1)                         //Checking v-shape formation\n            return -1; \n        int ans{0};\n        if(grid[i][j]==1)                         //If there is a  \'\\\' board at index (i,j)\n        {\n            ans=fB(i+1,j+1,grid);              //Get answer if the ball falls from (i+1,j+1) index,recursive call\n            if(ans==-1)                          //As you got -1 as ans from the  (i+1,j+1) index means there is no way ball can \n                return ans;                     //come out of any column\n        }\n        if(grid[i][j]==-1)                   //if there is a \'/\' board at index (i,j)\n        {\n            ans=fB(i+1,j-1,grid);        //Get answer if the ball falls from (i+1,j-1) index,recursive call\n            if(ans==-1)                       //As you got -1 as ans from the  (i+1,j-11) index means there is no way ball can \n                return -1;                      //come out of any column\n        }\n        return ans;                 //return the column ball comes out of\n    }\n    vector<int> findBall(vector<vector<int>>& grid) {\n        vector<int> res(grid[0].size(),0);             //vector to store where each ball ends it journey\n        for(int i=0;i<grid[0].size();i++)                 //A ball being thrown from each column\n        {\n            res[i]=fB(0,i,grid);                  //Starting index of ith ball being (0,i)\n        }\n        return res;\n    }\n};\n
['Recursion', 'C']