Find the Number of Ways to Place People 2 โ€” Algorithm Visualization & Coding Challenge

Choose Your Learning Path

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.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

๐Ÿ“– Passive Learning

Read the full explanation, examples, and starter code at your own pace.

๐ŸŽฏ Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

๐Ÿง  Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

๐Ÿ“– Select Passive to activate

Understanding Find the Number of Ways to Place People 2
Detailed explanation and reference materials
Problem Overview

๐Ÿงฉ Find the Number of Ways to Place People II

Problem #3027 | Difficulty: ๐Ÿ”ด Hard


๐Ÿ“ Problem Description

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

๐Ÿงญ Direction Definition

  • Right direction: positive x-axis (increasing x-coordinate)
  • Left direction: negative x-axis (decreasing x-coordinate)
  • Up direction: positive y-axis (increasing y-coordinate)
  • Down direction: negative y-axis (decreasing y-coordinate)

๐ŸŽฏ Problem Statement

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
  • Bob's position as the lower right corner

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.


๐Ÿ’ก Examples

Example 1

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.

Example 2

Input: points = [[6,2],[4,4],[2,6]]

Output: 2

Explanation: There are two valid ways to place Alice and Bob:

  • ๐ŸŸข Alice at (4, 4) and Bob at (6, 2)
  • ๐ŸŸข Alice at (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.

Example 3

Input: points = [[3,1],[1,3],[1,1]]

Output: 2

Explanation: Two valid placements:

  • ๐ŸŸข Alice at (1, 1) and Bob at (3, 1)
  • ๐ŸŸข Alice at (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!


๐Ÿ”’ Constraints

  • 2 <= n <= 1000
  • points[i].length == 2
  • -10^9 <= points[i][0], points[i][1] <= 10^9
  • All points are distinct

โš ๏ธ Important Notes

Alice 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:

  • With Alice at (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
  • With Alice at (1, 3) and Bob at (1, 1): Bob's position is not the lower right corner of the fence

๐ŸŽจ Key Insights

  1. Alice must be at the upper-left corner
  2. Bob must be at the lower-right corner
  3. No other person should be inside or on the fence boundary
  4. For Alice to be upper-left and Bob to be lower-right:
    • Alice's x-coordinate โ‰ค Bob's x-coordinate
    • Alice's y-coordinate โ‰ฅ Bob's y-coordinate

๐Ÿ’ญ Approach Strategy

For each pair of points (Alice, Bob):

  1. Check if Alice can be upper-left and Bob can be lower-right
  2. Verify no other points fall within or on the fence boundary
  3. Count all valid pairs

Topics: Array Math Enumeration Geometry

โ€” Written by Saurabh Patil โ€ข B.Tech CSE โ€ข Software Developer

Categories
2d-arrays
sorting
java
Reference Link
https://drawtocode.org/problems/find-the-number-of-ways-to-place-people-2

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now ๐ŸŽฎ

๐ŸŽฏ Select Challenge to activate

๐Ÿง  Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly ๐Ÿงฉ

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 Algorithm

Don't Know Current Algorithm ? ย 

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

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.

DrawToCode โ€” Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

Visualization Boardย |ย Problemsย |ย Aboutย |ย Privacy Policy
EmailLinkedInTwitterInstagramGitHub
ยฉ 2026 DrawToCode. All rights reserved.