Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Self Crossing

XPChallenge Points: 30
levelLevel: Hard

You are given an array distance where each element represents how many units you walk in the current direction.
You begin at position (0, 0) facing north.
After every move, you rotate 90 degrees counter-clockwise (north → west → south → east → …).

Your task is to determine whether, during this sequence of moves, the path ever overlaps or intersects any segment you have already drawn.

Return true if the path intersects itself at least once; otherwise, return false.

Example 1:

Input: distance = [3, 3, 3, 2]

Output: false

Explanation:

The fourth move brings the path into a previous segment.

Example 2:

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

Output: true

Explanation:

The path loops back to a previously visited point.

Example 3:

Input: distance = [2, 3, 4, 5]

Output: false

Explanation:

No intersections occur; the path continues outward.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result