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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:51:56+00:00 2026-06-08T16:51:56+00:00

I have encountered an inoridinary problem that given a NxM 0-1 matrix and a

  • 0

I have encountered an inoridinary problem that given a NxM 0-1 matrix and a number K(<=NxM) and I have to find a minimal subrectangle area of that 0-1 matrix with at least K 1’s in inside that subrectangle. Furthermore it’s area(the product of both dimensions) should be minimized.

For example:
00000
01010
00100
01010
00000
K = 3

So I can find a subrectangle with minimal area 6 that contains 3 1’s inside.
10
01
10

NOTE that the target subrectangle that I mean should contains consecutive numbers of rows and columns from the original 0-1 matrix.

  • 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-06-08T16:51:57+00:00Added an answer on June 8, 2026 at 4:51 pm
    Compute cumulative sum of rows R[i,j] and columns C[i,j].
    For top-left corner (i,j) of each possible sub-rectangle:
       Starting from a single-row sub-rectangle (n=i),
       Search the last possible column for this sub-rectangle (m).
       While m>=j:
         While there are more than 'k' "ones" in this sub-rectangle:
           If this is the smallest sub-rectangle so far, remember it.
           Remove column (--m).
           This decreases the number of "ones" by C[m+1,n]-C[m+1,j-1].
         Add next row (++n).
         This increases the number of "ones" by R[m,n]-R[i-1,n].
    

    Time complexity is O(NM(N+M)).

    Two nested loops may be optimized by changing linear search to binary search (to process skinny sub-rectangles faster).

    Also it is possible (after adding a row/a column to the sub-rectangle) to decrease in O(1) time the number of columns/rows in such a way that the area of this sub-rectangle is not larger than the area of the best-so-far sub-rectangle.

    Both these optimizations require calculation of any sub-rectangle weight in O(1). To make it possible, pre-calculate cumulative sum of all elements for sub-rectangles [1..i,1..j] (X[i,j]). Then the weight of any sub-rectangle [i..m,j..n] is computed as X[m,n]-X[i-1,n]-X[m,j-1]+X[i-1,j-1].


    Compute cumulative sum of columns C[i,j].
    For any starting row (k) of possible sub-rectangle:
      For any ending row (l) of possible sub-rectangle:
        Starting column (m = 1).
        Ending column (n = 1).
        While n is not out-of-bounds
          While there are less than 'k' "ones" in sub-rectangle [k..l,m..n]:
            Add column (++n).
            This increases the number of "ones" by C[l,n]-C[k-1,n].
          If this is the smallest sub-rectangle so far, remember it.
          Remove column (++m).
          This decreases the number of "ones" by C[l,m-1]-C[k-1,m-1].
    

    Time complexity is O(N2M).

    Loop by ‘l’ may be terminated when all sub-rectangles, processed inside it, are single-column sub-rectangles (too many rows) or when no sub-rectangles, processed inside it, contain enough “ones” (not enough rows).

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

Sidebar

Related Questions

I have encountered a most annoying problem that occurs on the PWD variable when
I have encountered a problem in my program that has me somewhat stumped. I
I have encountered a problem i cannot find a solution. I am using a
I have encountered a problem that I have never seen before. Usually, when I
I have encountered a wierd problem, I have a class that appears in the
I have encountered a problem. When I use jQuery to load a page that
I have encountered a problem where in my database I have a column that
I have encountered a problem that I have not come accross yet when setting
I have encountered a problem in my application and realized that I could fix
I have encountered a problem that I want to define a map, that is

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.