Easy to understand C++ solution | Sumeet Yadav
IntuitionThe problem simulates how balls traverse through a grid of inclines. Each cell in the grid either redirects the ball to the right (1) or left (-1). The
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem simulates how balls traverse through a grid of inclines. Each cell in the grid either redirects the ball to the right (1) or left (-1). The goal is to determine where each ball ends up after passing through the grid or if it gets stuck.

# Approach
<!-- Describe your approach to solving the problem. -->
1)Ball Simulation:
    ->For each column where a ball is dropped, simulate its movement through the grid row by row.
Use the direction given by the cell value (1 or -1) to determine the next column.
2)Edge Cases:
    ->If the ball attempts to move outside the grid bounds, it gets stuck.
If the ball encounters a "V" shape (e.g., moves right to a -1 or left to a 1), it gets stuck.
3)Iterate:
    ->Repeat the above simulation for each starting column and record the result.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(m⋅n) where 
𝑚
m is the number of rows and 
𝑛
n is the number of columns. Each ball is simulated across all rows.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1) (excluding the output vector), as the simulation operates in place without additional data structures.

# Code
```cpp []
class Solution {
public:
    int isPossible(vector<vector<int>>& grid, int col) {
        int rows = grid.size();
        int cols = grid[0].size();
        
        for (int row = 0; row < rows; ++row) {
            int dir = grid[row][col];
            // Check the next column based on the direction
            int nextCol = col + dir;

            // If out of bounds or the V-shape condition occurs, the ball gets stuck
            if (nextCol < 0 || nextCol >= cols || grid[row][nextCol] != dir) {
                return -1;
            }
            
            // Move to the next column
            col = nextCol;
        }
        
        // Ball successfully exits at column `col`
        return col;
    }

    vector<int> findBall(vector<vector<int>>& grid) {
        int cols = grid[0].size();
        vector<int> ans;

        // Simulate the ball drop for each column
        for (int i = 0; i < cols; ++i) {
            ans.push_back(isPossible(grid, i));
        }

        return ans;
    }
};

```
['C++']