# Self Crossing Problem

## Description

LeetCode Problem 335.

You are given an array of integers distance.

You start at point (0,0) on an X-Y plane and you move distance meters to the north, then distance meters to the west, distance meters to the south, distance meters to the east, and so on. In other words, after each move, your direction changes counter-clockwise.

Return true if your path crosses itself, and false if it does not.

Example 1:

``````1
2
Input: distance = [2,1,1,2]
Output: true
``````

Example 2:

``````1
2
Input: distance = [1,2,3,4]
Output: false
``````

Example 3:

``````1
2
Input: distance = [1,1,1,1]
Output: true
``````

Constraints:

• 1 <=distance.length <= 10^5
• 1 <=distance[i] <= 10^5

## Sample C++ Code

``````1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
bool isSelfCrossing(vector<int>& x) {
x.insert(x.begin(), 4, 0);

int len = x.size();
int i = 4;

// outer spiral
for (; i < len && x[i] > x[i - 2]; i++);

if (i == len) return false;

// check border
if (x[i] >= x[i - 2] - x[i - 4]) {
x[i - 1] -= x[i - 3];
}

// inner spiral
for (i++; i < len && x[i] < x[i - 2]; i++);

return i != len;
}
};
``````