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:
C++
#include <bits/stdc++.h>
using namespace std;
void findend( int i, int j, vector<vector< int >> &a,
vector<vector< int >> &output, int index)
{
int x = a.size();
int y = a[0].size();
int flagc = 0;
int flagr = 0;
int n, m;
for (m = i; m < x; m++)
{
if (a[m][j] == 1)
{
flagr = 1;
break ;
}
if (a[m][j] == 5) continue ;
for (n = j; n < y; n++)
{
if (a[m][n] == 1)
{
flagc = 1;
break ;
}
a[m][n] = 5;
}
}
if (flagr == 1)
output[index].push_back(m-1);
else
output[index].push_back(m);
if (flagc == 1)
output[index].push_back(n-1);
else
output[index].push_back(n);
}
void get_rectangle_coordinates(vector<vector< int >> a)
{
int size_of_array = a.size();
vector<vector< int >> output;
int index = -1;
for ( int i = 0; i < size_of_array; i++)
{
for ( int j = 0; j < a[0].size(); j++)
{
if (a[i][j] == 0)
{
output.push_back({i, j});
index = index + 1;
findend(i, j, a, output, index);
}
}
}
cout << "[" ;
int aa = 2, bb = 0;
for ( auto i:output)
{
bb = 3;
cout << "[" ;
for ( int j:i)
{
if (bb)
cout << j << ", " ;
else
cout << j;
bb--;
}
cout << "]" ;
if (aa)
cout << ", " ;
aa--;
}
cout << "]" ;
}
int main()
{
vector<vector< int >> 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);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void
findend( int i, int j, int [][] a,
ArrayList<ArrayList<Integer> > output,
int index)
{
int x = a.length;
int y = a[ 1 ].length;
int flagc = 0 ;
int flagr = 0 ;
int n = 0 , m = 0 ;
for (m = i; m < x; m++) {
if (a[m][j] == 1 ) {
flagr = 1 ;
break ;
}
if (a[m][j] == 5 )
continue ;
for (n = j; n < y; n++) {
if (a[m][n] == 1 ) {
flagc = 1 ;
break ;
}
a[m][n] = 5 ;
}
}
if (flagr == 1 ) {
var arr = output.get(index);
arr.add(m - 1 );
output.set(index, arr);
}
else
{
var arr = output.get(index);
arr.add(m);
output.set(index, arr);
}
if (flagc == 1 ) {
var arr = output.get(index);
arr.add(n - 1 );
output.set(index, arr);
}
else
{
var arr = output.get(index);
arr.add(n);
output.set(index, arr);
}
}
static void get_rectangle_coordinates( int [][] a)
{
int size_of_array = a.length;
ArrayList<ArrayList<Integer> > output
= new ArrayList<ArrayList<Integer> >();
int index = - 1 ;
for ( int i = 0 ; i < size_of_array; i++) {
for ( int j = 0 ; j < a[ 0 ].length; j++) {
if (a[i][j] == 0 ) {
output.add( new ArrayList<Integer>(
Arrays.asList(i, j)));
index = index + 1 ;
findend(i, j, a, output, index);
}
}
}
System.out.print( "[" );
int aa = 2 , bb = 0 ;
for (var i : output) {
bb = 3 ;
System.out.print( "[" );
for ( int j : i) {
if (bb > 0 )
System.out.print(j + ", " );
else
System.out.print(j);
bb--;
}
System.out.print( "]" );
if (aa > 0 )
System.out.print( ", " );
aa--;
}
System.out.print( "]" );
}
public static void main(String[] args)
{
int [][] 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);
}
}
|
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)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findend( int i, int j, int [, ] a,
List<List< int >> output, int index)
{
int x = a.GetLength(0);
int y = a.GetLength(1);
int flagc = 0;
int flagr = 0;
int n = 0, m = 0;
for (m = i; m < x; m++)
{
if (a[m, j] == 1)
{
flagr = 1;
break ;
}
if (a[m, j] == 5) continue ;
for (n = j; n < y; n++)
{
if (a[m, n] == 1)
{
flagc = 1;
break ;
}
a[m, n] = 5;
}
}
if (flagr == 1)
output[index].Add(m-1);
else
output[index].Add(m);
if (flagc == 1)
output[index].Add(n-1);
else
output[index].Add(n);
}
static void get_rectangle_coordinates( int [,] a)
{
int size_of_array = a.GetLength(0);
List<List< int >> output = new List<List< int >>();
int index = -1;
for ( int i = 0; i < size_of_array; i++)
{
for ( int j = 0; j < a.GetLength(1); j++)
{
if (a[i, j] == 0)
{
output.Add( new List< int >( new int [] {i, j}));
index = index + 1;
findend(i, j, a, output, index);
}
}
}
Console.Write( "[" );
int aa = 2, bb = 0;
foreach ( var i in output)
{
bb = 3;
Console.Write( "[" );
foreach ( int j in i)
{
if (bb > 0)
Console.Write(j + ", " );
else
Console.Write (j);
bb--;
}
Console.Write ( "]" );
if (aa > 0)
Console.Write( ", " );
aa--;
}
Console.Write( "]" );
}
public static void Main( string [] args)
{
int [, ] 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);
}
}
|
Javascript
<script>
function findend(i,j,a,output,index){
let x = a.length
let y = a[0].length
let m,n;
let flagc = 0
let flagr = 0
for (m=i;m<x;m++){
if (a[m][j] == 1){
flagr = 1
break
}
if (a[m][j] == 5)
pass
for (n=j;n<y;n++){
if (a[m][n] == 1){
flagc = 1
break
}
a[m][n] = 5
}
}
if (flagr == 1)
output[index].push( m-1)
else
output[index].push(m)
if (flagc == 1)
output[index].push(n-1)
else
output[index].push(n)
}
function get_rectangle_coordinates(a){
let size_of_array = a.length
let output = []
let index = -1
for (let i=0;i<size_of_array;i++){
for (let j=0;j<a[0].length;j++){
if (a[i][j] == 0){
output.push([i, j])
index = index + 1
findend(i, j, a, output, index)
}
}
}
console.log(output)
}
let 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)
</script>
|
Output
[[2, 3, 3, 5], [3, 1, 5, 1], [5, 3, 6, 5]]
Time Complexity: O(x*y).
Auxiliary Space: O(x*y).
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Asked in Intuit
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!