How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Problem #3027 | Difficulty: ๐ด Hard
You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].
You have to place n people, including Alice and Bob, at these points such that there is exactly one person at every point.
Alice wants to be alone with Bob, so Alice will build a rectangular fence with:
Note: The fence might not enclose any area (it can be a line)
If any person other than Alice and Bob is either inside the fence or on the fence, Alice will be sad. ๐ข
Return the number of pairs of points where you can place Alice and Bob such that Alice does not become sad.
Input: points = [[1,1],[2,2],[3,3]]
Output: 0
Explanation: There is no way to place Alice and Bob such that Alice can build a fence with Alice's position as the upper left corner and Bob's position as the lower right corner.
Input: points = [[6,2],[4,4],[2,6]]
Output: 2
Explanation: There are two valid ways to place Alice and Bob:
(4, 4) and Bob at (6, 2)(2, 6) and Bob at (4, 4)โ Invalid: Alice at (2, 6) and Bob at (6, 2) because the person at (4, 4) will be inside the fence.
Input: points = [[3,1],[1,3],[1,1]]
Output: 2
Explanation: Two valid placements:
(1, 1) and Bob at (3, 1)(1, 3) and Bob at (1, 1)โ Invalid: Alice at (1, 3) and Bob at (3, 1) because the person at (1, 1) will be on the fence.
๐ก Note: It doesn't matter if the fence encloses any area - the fence can be a line!
2 <= n <= 1000points[i].length == 2-10^9 <= points[i][0], points[i][1] <= 10^9Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner.
For example, Alice cannot build either of the fences with four corners (1, 1), (1, 3), (3, 1), and (3, 3), because:
(3, 3) and Bob at (1, 1): Alice's position is not the upper left corner and Bob's position is not the lower right corner(1, 3) and Bob at (1, 1): Bob's position is not the lower right corner of the fenceFor each pair of points (Alice, Bob):
Topics: Array Math Enumeration Geometry
๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.