Single Traversal || O(N) || Fast 💯|| C++✅
IntuitionThe logic is to calculate the maximum possible displacement in a 2D plane based on movements in four directions (N, S, E, W). Opposite movements cancel
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The logic is to calculate the maximum possible displacement in a 2D plane based on movements in four directions (N, S, E, W). Opposite movements cancel each other out (e.g., N vs. S, W vs. E), so we first determine the net movement in each axis. The formula max(N, S) + max(W, E) - min(N, S) - min(W, E) gives the effective movement. Additionally, we can use up to k extra steps to optimize weaker movements, maximizing the overall distance by adjusting the imbalance in movement directions. This ensures the farthest possible position is reached.

# Approach
<!-- Describe your approach to solving the problem. -->
Traverse through the string once only and keep noting number of east , west , north and south and at every step of traversal calculate maximum possible distance from the source via the formula
change = min(verticalmin + horizontalmin , k );
soln = max(soln, (verticalmax + horimax - vertimin - horimin + 2*change))

# Complexity
- Time complexity:
O(N)

- Space complexity:
O(1)

# Code
```cpp []
class Solution {
public:
    int maxDistance(string st, int k) {
        int n = 0 , s = 0 , e = 0 , w = 0;
        int soln = INT_MIN;
        for(int i = 0 ; i < st.size() ; i++ ){
            if(st[i] == 'N') n++;
            else if(st[i] == 'E') e++;
            else if(st[i] == 'W') w++;
            else s++;

            int vertimax = max(n,s);
            int vertimin = min(n,s);
            int horimax = max(e,w);
            int horimin = min(e,w);

            int change = min(vertimin + horimin , k );
            soln = max(soln, (vertimax + horimax - vertimin - horimin + 2*change));
        }
        return soln;
    }
};
```

![gato.png](https://assets.leetcode.com/users/images/1b586cc6-9aec-4c80-a87f-bc6f92d9e94c_1738689441.0420036.png)


['C++']