BackhardGraphsAtlassianMorgan Stanley

Convex Hull Boundary Engine 2 Solution

Problem Statement

Given a set of points in 2D space, find the convex hull boundary using the Heavy-Light Decomposition algorithm.

Example 1
Input
[[1, 1], [2, 1], [2, 2], [1, 3]]
Output
[1, 1, 2, 2]

Explanation: Step 1: Sort the points by their polar angles with respect to the point [1, 1]. Step 2: Initialize the convex hull with the first two points [1, 1] and [2, 1]. Step 3: Iterate over the remaining points and add them to the convex hull if they are on the right side of the line formed by the last two points in the hull. Step 4: Return the convex hull.

Example 2
Input
[[1, 1], [2, 1], [1, 3], [2, 2]]
Output
[1, 1, 2, 1]

Explanation: Step 1: Sort the points by their polar angles with respect to the point [1, 1]. Step 2: Initialize the convex hull with the first two points [1, 1] and [2, 1]. Step 3: Iterate over the remaining points and add them to the convex hull if they are on the right side of the line formed by the last two points in the hull. Step 4: Return the convex hull.

Constraints

  • 1 <= N <= 2 * 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity: O(N log N) or O(N log^2 N)
  • Space Complexity: O(N)
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Convex Hull Boundary Engine 2 — Problem Statement & Solution Guide

GraphsHardHeavy-Light Decomposition
TimeO(n)
|
SpaceO(1)

Problem Description

Given a set of points in 2D space, find the convex hull boundary using the Heavy-Light Decomposition algorithm.

Examples

Example 1

Input

[[1, 1], [2, 1], [2, 2], [1, 3]]

Output

[1, 1, 2, 2]

Explanation: Step 1: Sort the points by their polar angles with respect to the point [1, 1]. Step 2: Initialize the convex hull with the first two points [1, 1] and [2, 1]. Step 3: Iterate over the remaining points and add them to the convex hull if they are on the right side of the line formed by the last two points in the hull. Step 4: Return the convex hull.

Example 2

Input

[[1, 1], [2, 1], [1, 3], [2, 2]]

Output

[1, 1, 2, 1]

Explanation: Step 1: Sort the points by their polar angles with respect to the point [1, 1]. Step 2: Initialize the convex hull with the first two points [1, 1] and [2, 1]. Step 3: Iterate over the remaining points and add them to the convex hull if they are on the right side of the line formed by the last two points in the hull. Step 4: Return the convex hull.

Constraints

  • 1 <= N <= 2 * 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity: O(N log N) or O(N log^2 N)
  • Space Complexity: O(N)

Optimal Approach & Strategy

Use Heavy-Light Decomposition to process subproblems in O(N log N) time and O(N) auxiliary memory.

Brute Force Approach

Evaluate state space permutations in O(2^N) or O(N^3) time.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function convexHull(points) {
  points.sort((a, b) => (a[0] - a[1]) - (b[0] - b[1]));
  let hull = [points[0], points[1]];
  for (let i = 2; i < points.length; i++) {
    while (hull.length > 1 && crossProduct(hull[hull.length - 2], hull[hull.length - 1], points[i]) <= 0) {
      hull.pop();
    }
    hull.push(points[i]);
  }
  return hull;
}

function crossProduct(p1, p2, p3) {
  return (p2[0] - p1[0]) * (p3[1] - p1[1]) - (p2[1] - p1[1]) * (p3[0] - p1[0]);
}

Asked in Top Tech Interviews

AtlassianMorgan Stanley

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.