Find the Number of Ways to Place People II - Problem

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].

We define the right direction as positive x-axis (increasing x-coordinate) and the left direction as negative x-axis (decreasing x-coordinate). Similarly, we define the up direction as positive y-axis (increasing y-coordinate) and the down direction as negative y-axis (decreasing y-coordinate).

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 Alice's position as the upper left corner and Bob's position as the lower right corner of the fence.

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 on building the fence.

Note that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner.

Input & Output

Example 1 — Basic Case
$ Input: points = [[1,1],[2,2],[3,3]]
Output: 0
💡 Note: No valid Alice-Bob pairs exist. For any pair where Alice could be upper-left and Bob lower-right, the third point would always fall inside or on their rectangle boundary.
Example 2 — Valid Pairs
$ Input: points = [[6,2],[4,4],[2,6]]
Output: 2
💡 Note: Two valid pairs: Alice at (2,6) with Bob at (6,2) creates empty rectangle, and Alice at (2,6) with Bob at (4,4) also creates empty rectangle.
Example 3 — Edge Case
$ Input: points = [[3,1],[1,3],[1,1],[2,2]]
Output: 1
💡 Note: Only Alice at (1,3) and Bob at (3,1) form a valid pair with no other people inside their rectangle.

Constraints

  • 2 ≤ points.length ≤ 50
  • -50 ≤ points[i][0], points[i][1] ≤ 50
  • All points[i] are distinct

Visualization

Tap to expand
Find Valid Alice-Bob Rectangle PlacementsAlice (1,4)Bob (4,2)Other (2,3)❌ Rectangle contains another personAlice (5,4)Bob (6,2)✅ Empty rectangle - Valid pair!
Understanding the Visualization
1
Input Points
Given coordinates on 2D plane
2
Form Rectangles
Alice upper-left, Bob lower-right
3
Count Empty
Rectangles with no other people inside
Key Takeaway
🎯 Key Insight: Count rectangles where Alice is upper-left, Bob is lower-right, and no other people are inside
Asked in
Google 15 Microsoft 12
12.0K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen