You are given an array of integers distance.

You start at point (0,0) on an X-Y plane and you move distance[0] meters to the north, then distance[1] meters to the west, distance[2] meters to the south, distance[3] 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.

&nbsp;
Example 1:

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


Example 2:

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


Example 3:

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


&nbsp;
Constraints:


	1 &lt;=&nbsp;distance.length &lt;= 105
	1 &lt;=&nbsp;distance[i] &lt;= 105


class Solution:
    def isSelfCrossing(self, x):
        n = len(x)
        if n < 4: return False
        for i in range(3, n):
            if x[i] >= x[i-2] and x[i-1] <= x[i-3]: return True
            if i >= 4 and x[i-1]==x[i-3] and x[i]+x[i-4]>=x[i-2]: return True
            if i >= 5 and 0<=x[i-2]-x[i-4]<=x[i] and 0<=x[i-3]-x[i-1]<=x[i-5]: return True
        return False
        
class Solution {

public boolean isSelfCrossing(int[] x) {
boolean arm = false;
boolean leg = false;
for (int i = 2; i < x.length; ++i) {
int a = f(x, i - 2) - f(x, i - 4);
int b = f(x, i - 2);

if (arm && x[i] >= b)          return true;  // cross [i - 2]
if (leg && x[i] >= a && a > 0) return true;  // cross [i - 4]

if (x[i] < a)       arm = true;
else if (x[i] <= b) leg = true;
}
return false;
}
private int f(int[] x, int index) {
return (index < 0) ? 0 : x[index];
}
}
class Solution {
public:
    bool isSelfCrossing(vector<int>& distance) {
        if (distance.size() <= 3) return false; //only can have intersection with more than 4 lines

        distance.insert(distance.begin(), 0); //for the edge case: line i intersect with line i-4 at (0, 0)
        for (int i = 3; i < distance.size(); i++) {
            //check line i-3
            if (distance[i - 2] <= distance[i] && distance[i - 1] <= distance[i - 3]) return true;

            //check line i-5
            if (i >= 5) {
                if (distance[i - 1] <= distance[i - 3] && distance[i - 1] >= distance[i - 3] - distance[i - 5] 
                    && distance[i - 2] >= distance[i - 4] && distance[i - 2] <= distance[i - 4] + distance[i])
                    return true;
            }
        }
        return false;
    }
};
class Solution {

public boolean isSelfCrossing(int[] x) {
boolean arm = false;
boolean leg = false;
for (int i = 2; i < x.length; ++i) {
int a = f(x, i - 2) - f(x, i - 4);
int b = f(x, i - 2);

if (arm && x[i] >= b)          return true;  // cross [i - 2]
if (leg && x[i] >= a && a > 0) return true;  // cross [i - 4]

if (x[i] < a)       arm = true;
else if (x[i] <= b) leg = true;
}
return false;
}
private int f(int[] x, int index) {
return (index < 0) ? 0 : x[index];
}
}
Self Crossing