335. Self Crossing LeetCode Solution
In this guide, you will get 335. Self Crossing LeetCode Solution with the best time and space complexity. The solution to Self Crossing problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Self Crossing solution in C++
- Self Crossing solution in Java
- Self Crossing solution in Python
- Additional Resources

Problem Statement of Self Crossing
You are given an array of integers distance.
You start at the 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 or false if it does not.
Example 1:
Input: distance = [2,1,1,2]
Output: true
Explanation: The path crosses itself at the point (0, 1).
Example 2:
Input: distance = [1,2,3,4]
Output: false
Explanation: The path does not cross itself at any point.
Example 3:
Input: distance = [1,1,1,2,1]
Output: true
Explanation: The path crosses itself at the point (0, 0).
Constraints:
1 <= distance.length <= 105
1 <= distance[i] <= 105
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
335. Self Crossing LeetCode Solution in C++
class Solution {
public:
bool isSelfCrossing(vector<int>& x) {
if (x.size() <= 3)
return false;
for (int i = 3; i < x.size(); ++i) {
if (x[i - 2] <= x[i] && x[i - 1] <= x[i - 3])
return true;
if (i >= 4 && x[i - 1] == x[i - 3] && x[i - 2] <= x[i] + x[i - 4])
return true;
if (i >= 5 && x[i - 4] <= x[i - 2] && x[i - 2] <= x[i] + x[i - 4] &&
x[i - 1] <= x[i - 3] && x[i - 3] <= x[i - 1] + x[i - 5])
return true;
}
return false;
}
};
/* code provided by PROGIEZ */
335. Self Crossing LeetCode Solution in Java
class Solution {
public boolean isSelfCrossing(int[] x) {
if (x.length <= 3)
return false;
for (int i = 3; i < x.length; ++i) {
if (x[i - 2] <= x[i] && x[i - 1] <= x[i - 3])
return true;
if (i >= 4 && x[i - 1] == x[i - 3] && x[i - 2] <= x[i] + x[i - 4])
return true;
if (i >= 5 && x[i - 4] <= x[i - 2] && x[i - 2] <= x[i] + x[i - 4] && x[i - 1] <= x[i - 3] &&
x[i - 3] <= x[i - 1] + x[i - 5])
return true;
}
return false;
}
}
// code provided by PROGIEZ
335. Self Crossing LeetCode Solution in Python
class Solution:
def isSelfCrossing(self, x: list[int]) -> bool:
if len(x) <= 3:
return False
for i in range(3, len(x)):
if x[i - 2] <= x[i] and x[i - 1] <= x[i - 3]:
return True
if i >= 4 and x[i - 1] == x[i - 3] and x[i - 2] <= x[i] + x[i - 4]:
return True
if i >= 5 and x[i - 4] <= x[i - 2] and x[i - 2] <= x[i] + x[i - 4] and x[i - 1] <= x[i - 3] and x[i - 3] <= x[i - 1] + x[i - 5]:
return True
return False
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.