Python Program to Find all rectangles filled with 0
Last Updated :
28 Jul, 2022
We have one 2D array, filled with zeros and ones. We have to find the starting point and ending point of all rectangles filled with 0. It is given that rectangles are separated and do not touch each other however they can touch the boundary of the array.A rectangle might contain only one element.
Examples:
input = [
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1]
]
Output:
[
[2, 3, 3, 5], [3, 1, 5, 1], [5, 3, 6, 5]
]
Explanation:
We have three rectangles here, starting from
(2, 3), (3, 1), (5, 3)
Input = [
[1, 0, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 1, 0]
]
Output:
[
[0, 1, 0, 1], [1, 2, 1, 2], [2, 3, 3, 5],
[3, 1, 4, 1], [5, 3, 5, 6], [7, 2, 7, 2],
[7, 6, 7, 6]
]
Step 1: Look for the 0 row-wise and column-wise
Step 2: When you encounter any 0, save its position in output array and using loop change all related 0 with this position in any common number so that we can exclude it from processing next time.
Step 3: When you change all related 0 in Step 2, store last processed 0’s location in output array in the same index.
Step 4: Take Special care when you touch the edge, by not subtracting -1 because the loop has broken on the exact location.
Below is the implementation of above approach:
Python3
def findend(i,j,a,output,index):
x = len (a)
y = len (a[ 0 ])
flagc = 0
flagr = 0
for m in range (i,x):
if a[m][j] = = 1 :
flagr = 1
break
if a[m][j] = = 5 :
pass
for n in range (j, y):
if a[m][n] = = 1 :
flagc = 1
break
a[m][n] = 5
if flagr = = 1 :
output[index].append( m - 1 )
else :
output[index].append(m)
if flagc = = 1 :
output[index].append(n - 1 )
else :
output[index].append(n)
def get_rectangle_coordinates(a):
size_of_array = len (a)
output = []
index = - 1
for i in range ( 0 ,size_of_array):
for j in range ( 0 , len (a[ 0 ])):
if a[i][j] = = 0 :
output.append([i, j])
index = index + 1
findend(i, j, a, output, index)
print (output)
tests = [
[ 1 , 1 , 1 , 1 , 1 , 1 , 1 ],
[ 1 , 1 , 1 , 1 , 1 , 1 , 1 ],
[ 1 , 1 , 1 , 0 , 0 , 0 , 1 ],
[ 1 , 0 , 1 , 0 , 0 , 0 , 1 ],
[ 1 , 0 , 1 , 1 , 1 , 1 , 1 ],
[ 1 , 0 , 1 , 0 , 0 , 0 , 0 ],
[ 1 , 1 , 1 , 0 , 0 , 0 , 1 ],
[ 1 , 1 , 1 , 1 , 1 , 1 , 1 ]
]
get_rectangle_coordinates(tests)
|
Output:
[[2, 3, 3, 5], [3, 1, 5, 1], [5, 3, 6, 5]]
Time Complexity: O(x*y).
Auxiliary Space: O(x*y).
Please refer complete article on Find all rectangles filled with 0 for more details!
Similar Reads
Python Program to Find Area of Rectangle
The task of calculating the Area of a Rectangle in Python involves taking the length and width as input, applying the mathematical formula for the area of a rectangle, and displaying the result. Area of Rectangle Formula :Area = Width * Height Where: Length is the longer side of the rectangle.Width
2 min read
Python3 Program to Find if there is a subarray with 0 sum
Given an array of positive and negative numbers, find if there is a subarray (of size at-least one) with 0 sum. Examples : Input: {4, 2, -3, 1, 6}Output: true Explanation:There is a subarray with zero sum from index 1 to 3. Input: {4, 2, 0, 1, 6}Output: true Explanation:There is a subarray with zero
3 min read
Python Program for Maximum size square sub-matrix with all 1s
Write a Python program for a given binary matrix, the task is to find out the maximum size square sub-matrix with all 1s. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Approach: Let the given binary matrix be M[R][C]. The idea of the algorithm is to construct an
4 min read
How to Pad a String to a Fixed Length with Zeros in Python
Padding a string to a fixed length with zeros in Python is basically adding leading zeros until it reaches the desired size. Using zfill() MethodThe simplest way to pad a string with zeros is by using Pythonâs built-in zfill() method. This method adds zeros to the left of a string until it reaches t
2 min read
How To Return 0 With Divide By Zero In Python
Dividing a number by zero is a big problem in math, and it can cause errors that suddenly stop your Python program. However, what if you could smoothly deal with this issue and make your program return a specific value, such as 0, instead of crashing? This article looks into different methods to acc
3 min read
Python Program to Sort an array of 0s, 1s and 2s
Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last. Examples: Input : {0, 1, 2, 0, 1, 2, 2, 2, 2, 1} Output : {0, 0, 1, 1, 1, 2, 2, 2, 2, 2} Input : {0, 1, 1, 0, 1, 2, 1, 2, 0,
2 min read
Techniques to Find Consecutive 1s or 0s in a Python String
We are given a binary string and a number m, and we have to check if the string has m consecutive 1âs or 0âs. Below are a few examples to understand the problem statement clearly. Examples:Input: str = â001001â, m = 2 Output: TrueExplanation: the string have 2 consecutive 0s Input: str = â1000000001
4 min read
Python3 Program for Segregate 0s and 1s in an array
You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Traverse array only once. Input array = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] Output array = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] Method 1 (Count 0s or 1s) Thanks to Naveen for suggesting this method
3 min read
Rearrange a Binary String with Alternating 0s and 1s in Python
We are given a binary string and we have to check whether it is possible to rearrange the string with alternate 0s and 1s. Below are a few examples to understand the problem statement clearly. Examples:Input: "1011"Output: FalseExplanation: We canât rearrange the string such that it has alternate 0s
2 min read
Python Program for Segregate 0s and 1s in an array
You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Traverse array only once. Example: Input array = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] Output array = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] Recommended: Please solve it on âPRACTICE â first, before mo
3 min read