Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6657725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:48:20+00:00 2026-05-26T01:48:20+00:00

Given the following filled x, y coordinates: 0, 0 0, 1 0, 2 1,

  • 0

Given the following filled x, y coordinates:

0, 0
0, 1
0, 2
1, 0 
1, 1
1, 2
2, 0
2, 1
2, 2
4, 0
4, 1
5, 0
5, 1

How do I write an SQL query to determine all the filled rectangles? A rectangle is defined by its top left and bottom right corners.

Desired Results

x1 | y1 | x2 | y2 
 0    0   2    2 
 0    4   1    5

Because

+---+---+---+---+
|   | 0 | 1 | 2 |
+---+---+---+---+
| 0 | X | X | X |
| 1 | X | X | X |
| 2 | X | X | X |
| 3 |   |   |   |
| 4 | X | X |   |
| 5 | X | X |   |
+---+---+---+---+
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:48:20+00:00Added an answer on May 26, 2026 at 1:48 am

    Interesting puzzle, solution edited with ideas from ypercube’s answer:

    declare @t table (x int, y int);
    insert @t (x,y) values (0, 0), (0, 1), (0, 2), (1, 0),  (1, 1), (1, 2), 
                           (2, 0), (2, 1), (2, 2), (4, 0), (4, 1), (5, 0), (5, 1);
    
    ; with  all_rectangles as
            (
            select  lt.x as x1
            ,       lt.y as y1
            ,       rt.x as x2
            ,       lb.y as y2
            from    @t lt -- Left top
            join    @t rt -- Right top
            on      rt.y = lt.y -- Must share top
                    and rt.x > lt.x
            join    @t lb -- Left bottom
            on      lb.x = lt.x -- Must share left
                    and lb.y > lt.y
            join    @t rb -- Right bottom (limits resultset)
            on      rb.x = rt.x -- Must share right
                    and rb.y = lb.y -- Must share bottom
            )
    ,       filled_rectangles as
            (
            select  rect.x1
            ,       rect.y1
            ,       rect.x2
            ,       rect.y2
            from    all_rectangles rect
            join    @t crossed
            on      crossed.x between rect.x1 and rect.x2
                    and crossed.y between rect.y1 and rect.y2
            group by
                    rect.x1
            ,       rect.y1
            ,       rect.x2
            ,       rect.y2
            having  count(*) = 
                    (rect.x2 - rect.x1 + 1) * (rect.y2 - rect.y1 + 1)
            )
    select  *
    from    filled_rectangles rect
    where   not exists
            (
            select  *
            from    filled_rectangles bigger
            where   bigger.x1 <= rect.x1 and rect.x2 <= bigger.x2
                    and bigger.y1 <= rect.y1 and rect.y2 <= bigger.y2
                    and (rect.x1 <> bigger.x1 or rect.x2 <> bigger.x2 
                    or rect.y1 <> bigger.y1 or rect.y2 <> bigger.y2)
            );
    

    It first builds a list of all possible rectangles. Then it demands that the number of filled positions matches the total number of positions (the area of the rectangle.) Finally, it demands that there’s no other rectangle that entirely covers the rectangle.

    You might have to adopt it for PostgreSQL, but the idea should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following: List<List<Option>> optionLists; what would be a quick way to determine the
Given following Ruby statements: (Read input and store each word in array removing spaces
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given the following XML: <current> <login_name>jd</login_name> </current> <people> <person> <first>John</first> <last>Doe</last> <login_name>jd</login_name> </preson> <person>
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Given the following: declare @a table ( pkid int, value int ) declare @b
Given the following XML structure <html> <body> <div> <span>Test: Text2</span> </div> <div> <span>Test: Text3</span>
Given the following: &row->count Would &(row->count) be evaluated or (&row)->count be evaluated in C++?
Given the following code (that doesn't work): while True: # Snip: print out current
Given the following canvas: <Canvas> <Canvas.LayoutTransform> <ScaleTransform ScaleX=1 ScaleY=1 CenterX=.5 CenterY=.5 /> </Canvas.LayoutTransform> <Button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.