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 6801459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:06:10+00:00 2026-05-26T19:06:10+00:00

Given a square matrix, where each cell is black or white. Design an algorithm

  • 0

Given a square matrix, where each cell is black or white. Design an algorithm to find the max sub-square such that all 4 borders are black.

I have O(n^2) algorithm:

Scan each column from left to right, for each cell in each column, scan each row to find the max sub-square with back borders.

Are there better solutions ?

thanks

  • 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-26T19:06:11+00:00Added an answer on May 26, 2026 at 7:06 pm

    O(n^2) is possible. I guess it is optimal, as you have n^2 cells.

    Notice that the top-left corner and the bottom-right corner of any square lie along the same diagonal.

    Now if we could process each diagonal in O(n) time, we would have an O(n^2) time algorithm.

    Say we have a candidate for a top-left corner. We can compute the number of continuous black cells below it, and to the right of it and take the minimum of the two and call it T.

    For a bottom-right candidate, we can compute the number of continuous black cells to the left of it, and to the top and take the minimum of the two, call it B.

    Once we have the two numbers T and B, we can tell if the given top-left, bottom-right candidate actually give us a square with all black borders.

    Now those two numbers can be computed for each cell, in O(n^2) time by making four passes through the whole matrix (How?).

    So let us assume that is done.

    Now consider a diagonal. Our aim is to find a top-left,bottom-right pair along this diagonal in O(n) time.

    Let us assume we have the T’s computed in an array T[1…m] where m is the length of the diagonal. Similarly we have an array B[1…m]. T[1] corresponds to the top-left end of the diagonal and T[m] to the bottom-right. Similarly with B.

    Now we process the diagonal as follows, for each top-left candidate i, we try to find a bottom-right candidate j which will give the largest square. Notice that j >= i. Also notice that if (i,j) is a candidate, then (i’,j) isn’t, where i’ > i.

    Note that i and j form a square if T[i] >= j-i+1 and B[j] >= j-i+1.

    i.e. T[i] +i - 1 >= j and B[j] -j - 1 >= -i.

    So we form new arrays such that TL[k] = T[k] + k -1 and BR[k] = B[k] -k - 1.

    So given the two new arrays TL and BR, and an i, we need to answer the following queries:

    What is the largest j such that TL[i] >= j and BR[j] >= -i ?

    Now suppose we were able to process BR for range maximum queries (can be done in O(m) time).

    Now given TL[i], in the range [i, TL[i]] we find the maximum value of BR, say BR[j1].

    Now if BR[j1] >= -i, we find the maximum of BR in the range [j1, TL[i]] and continue this way.

    Once we find the (TL[i],BR[j]) candidate, we can ignore the array BR[1…j] for future i.

    This allows us to process each diagonal in O(n) time, giving an O(n^2) total algorithm.

    I have left out a lot of details and given a sketch, as it was already too long. Feel free to edit this with clarifications.

    Phew.

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

Sidebar

Related Questions

When given a square matrix, what would be the best way to find all
Is there an algorithm to find the square root of a given number using
I'm trying to take the square root of a matrix. That is find the
I have some simple code that represents a graph using a square boolean matrix
Given a matrix A (not neccessarily square) with independent columns, I was able to
I'm trying to write a program in Java that when given an MxN matrix
Given an integer N I want to find two integers A and B that
To make it more specific, I need an algorithm (recursive or not) that, given
There is a square binary matrix that denotes connections in a bipartite graph. The
Given a square (described by x, y, width, height) and an angle (in radians)

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.