Skip to content
Discussion options

You must be logged in to vote

We need to find the smallest set of numbers that contains at least two elements from each given interval.

The key insight is to sort the intervals by their end points and use a greedy approach to select numbers that will cover as many intervals as possible.

Approach:

  1. Sort intervals by end point (ascending), and if end points are equal, sort by start point (descending)
  2. Maintain two variables to track the last two numbers selected
  3. For each interval:
    • If it already contains both last selected numbers, skip it
    • If it contains only one of them, add the largest number from the current interval
    • If it contains none of them, add the two largest numbers from the current interval

Let's implement …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Nov 20, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Nov 20, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants