# Find number of square of area Z which can be built in a matrix having blocked regions

In an N * N matrix, certain rows and columns are blocked. Find the number of squares of area Z which can be built in such a matrix with the given constraints:

1. Every cell will contribute 1 unit area and 1 cell belongs to exactly 1 sub – grid.
2. All cells should be unblocked cell.

The blocked rows and columns are represented by two arrays row[] and col[].

Examples:

Input: N = 8, Z = 4, row[] = {4, 6}, col[] = {3, 8}
Output: 6
Here is the required matrix:
1 1 X 2 2 2 2 X
1 1 X 2 2 2 2 X
1 1 X 2 2 2 2 X
X X X X X X X X
3 3 X 4 4 4 4 X
X X X X X X X X
5 5 X 6 6 6 6 X
5 5 X 6 6 6 6 X
Where X represents blocked cells. 1 – 6 represents unblocked rectangular regions.
Contribution to answer is as given :
Region 1: 1 region with area 4
Region 2: 2 regions with area 4
Region 3: No region with area 4
Region 4: No region with area 4
Region 5: 1 region with area 4
Region 6: 2 regions with area 4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Here, the actual matrix will be divided into several rectangles. Regions can only be built in these rectangles. Suppose there exists a rectangle with dimensions R * L then the number of regions of area Z will be equal to (R / K) * (L / K) where K is the square root of Z because maximum square of dimension K which can fit in length L is L / K and same goes for the width. So, the task is to find the dimensions of all such rectangles and calculate the number of areas possible in it.
• To find the rectangles, first all the blocked rows and columns are already given in sorted order. Now, consider the region between the first blocked row and the first blocked column. This region is entirely unblocked. Similarly, a region between any two consecutive blocked column along with any two consecutive blocked row will form an unblocked region.
• So, basically any unblocked region will lie between any two consecutive blocked rows and any two consecutive blocked columns. We can store length between consecutive blocked rows and consecutive blocked columns and then we can take any length from row array and any length from column array and multiply them.
• Find all these unblocked regions and sum the number of squares of size K * K possible to fit in total.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to calculate the number of ` `// square areas of size K*K ` `int` `subgrids(``int` `N, ``int` `Z, ``int` `row[], ` `             ``int` `col[], ``int` `r, ``int` `c) ` `{ ` `    ``// Row array and column array to ` `    ``// store the lengths of differences ` `    ``// between consecutive rows/columns ` `    ``vector<``int``> conrow; ` `    ``vector<``int``> concol; ` ` `  `    ``int` `K = ``sqrt``(Z); ` ` `  `    ``// Fill the conrow vector ` `    ``conrow.push_back(row - 0 - 1); ` `    ``conrow.push_back(N + 1 - row[r - 1] - 1); ` `    ``for` `(``int` `i = 1; i < r; i++) { ` `        ``conrow.push_back(row[i] - row[i - 1] - 1); ` `    ``} ` ` `  `    ``// Fill the concol vector ` `    ``concol.push_back(col - 0 - 1); ` `    ``concol.push_back(N + 1 - col - 1); ` `    ``for` `(``int` `i = 1; i < c; i++) { ` `        ``concol.push_back(col[i] - col[i - 1] - 1); ` `    ``} ` ` `  `    ``int` `row_size = conrow.size(); ` `    ``int` `col_size = concol.size(); ` ` `  `    ``// To store the required answer ` `    ``int` `answer = 0; ` ` `  `    ``// Every pair of row size and column size ` `    ``// would result in an unblocked region ` `    ``for` `(``int` `i = 0; i < row_size; i++) { ` `        ``for` `(``int` `j = 0; j < col_size; j++) { ` `            ``int` `total = (concol[j] / K) ` `                        ``* (conrow[i] / K); ` `            ``answer += (total); ` `        ``} ` `    ``} ` ` `  `    ``return` `answer; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `N = 8, Z = 4; ` `    ``int` `row[] = { 4, 6 }; ` `    ``int` `col[] = { 3, 8 }; ` `    ``int` `r = ``sizeof``(row) / ``sizeof``(row); ` `    ``int` `c = ``sizeof``(col) / ``sizeof``(col); ` ` `  `    ``cout << subgrids(N, Z, row, col, r, c); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to calculate the number of ` `// square areas of size K*K ` `static` `int` `subgrids(``int` `N, ``int` `Z, ``int` `row[], ` `                    ``int` `col[], ``int` `r, ``int` `d) ` `{ ` `    ``// Row array and column array to ` `    ``// store the lengths of differences ` `    ``// between consecutive rows/columns ` `    ``Vector conrow = ``new` `Vector(); ` `    ``Vector concol = ``new` `Vector(); ` ` `  `    ``int` `K = (``int``) Math.sqrt(Z); ` ` `  `    ``// Fill the conrow vector ` `    ``conrow.add(row[``0``] - ``0` `- ``1``); ` `    ``conrow.add(N + ``1` `- row[r - ``1``] - ``1``); ` `    ``for` `(``int` `i = ``1``; i < r; i++) ` `    ``{ ` `        ``conrow.add(row[i] - row[i - ``1``] - ``1``); ` `    ``} ` ` `  `    ``// Fill the concol vector ` `    ``concol.add(col[``0``] - ``0` `- ``1``); ` `    ``concol.add(N + ``1` `- col[d - ``1``] - ``1``); ` `    ``for` `(``int` `i = ``1``; i < d; i++) ` `    ``{ ` `        ``concol.add(col[i] - col[i - ``1``] - ``1``); ` `    ``} ` ` `  `    ``int` `row_size = conrow.size(); ` `    ``int` `col_size = concol.size(); ` ` `  `    ``// To store the required answer ` `    ``int` `answer = ``0``; ` ` `  `    ``// Every pair of row size and column size ` `    ``// would result in an unblocked region ` `    ``for` `(``int` `i = ``0``; i < row_size; i++) ` `    ``{ ` `        ``for` `(``int` `j = ``0``; j < col_size; j++) ` `        ``{ ` `            ``int` `total = (concol.get(j) / K) *  ` `                        ``(conrow.get(i) / K); ` `            ``answer += (total); ` `        ``} ` `    ``} ` `    ``return` `answer; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `N = ``8``, Z = ``4``; ` `    ``int` `row[] = { ``4``, ``6` `}; ` `    ``int` `col[] = { ``3``, ``8` `}; ` `    ``int` `r = row.length; ` `    ``int` `d = col.length; ` ` `  `    ``System.out.print(subgrids(N, Z, row, col, r, d)); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python3

 `# Python3 implementation of the approach  ` `from` `math ``import` `sqrt ` ` `  `# Function to calculate the number of  ` `# square areas of size K*K  ` `def` `subgrids(N, Z, row, col, r, d) : ` `     `  `    ``# Row array and column array to ` `    ``# store the lengths of differences ` `    ``# between consecutive rows/columns ` `    ``conrow ``=` `[]; ` `    ``concol ``=` `[]; ` `     `  `    ``K ``=` `int``(sqrt(Z)); ` `     `  `    ``# Fill the conrow vector ` `    ``conrow.append(row[``0``] ``-` `0` `-` `1``) ` `    ``conrow.append(N ``+` `1` `-` `row[r ``-` `1``] ``-` `1``) ` `     `  `    ``for` `i ``in` `range``(``1``, r) : ` `        ``conrow.append(row[i] ``-` `row[i ``-` `1``] ``-` `1``); ` `         `  `    ``# Fill the concol vector ` `    ``concol.append(col[``0``] ``-` `0` `-` `1``) ` `    ``concol.append(N ``+` `1` `-` `col[d ``-` `1``] ``-` `1``) ` `     `  `    ``for` `i ``in` `range``(``1``, d) : ` `        ``concol.append(col[i] ``-` `col[i ``-` `1``] ``-` `1``); ` `         `  `    ``row_size ``=` `len``(conrow) ` `    ``col_size ``=` `len``(concol) ` ` `  `    ``# To store the required answer  ` `    ``answer ``=` `0` `     `  `    ``# Every pair of row size and column size ` `    ``# would result in an unblocked region ` `    ``for` `i ``in` `range``(row_size) : ` `        ``for` `j ``in` `range``(col_size) : ` `            ``total ``=` `(concol[j] ``/``/` `K) ``*` `\ ` `                    ``(conrow[i] ``/``/` `K) ` `            ``answer ``+``=` `(total) ` `             `  `    ``return` `answer ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``N ``=` `8``; Z ``=` `4` `    ``row ``=` `[ ``4``, ``6` `] ` `    ``col ``=` `[ ``3``, ``8` `]  ` `    ``r ``=` `len``(row) ` `    ``d ``=` `len``(col)  ` ` `  `    ``print``(subgrids(N, Z, row, col, r, d)) ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to calculate the number of ` `// square areas of size K*K ` `static` `int` `subgrids(``int` `N, ``int` `Z, ``int` `[]row, ` `                    ``int` `[]col, ``int` `r, ``int` `d) ` `{ ` `    ``// Row array and column array to ` `    ``// store the lengths of differences ` `    ``// between consecutive rows/columns ` `    ``List<``int``> conrow = ``new` `List<``int``>(); ` `    ``List<``int``> concol = ``new` `List<``int``>(); ` ` `  `    ``int` `K = (``int``) Math.Sqrt(Z); ` ` `  `    ``// Fill the conrow vector ` `    ``conrow.Add(row - 0 - 1); ` `    ``conrow.Add(N + 1 - row[r - 1] - 1); ` `    ``for` `(``int` `i = 1; i < r; i++) ` `    ``{ ` `        ``conrow.Add(row[i] - row[i - 1] - 1); ` `    ``} ` ` `  `    ``// Fill the concol vector ` `    ``concol.Add(col - 0 - 1); ` `    ``concol.Add(N + 1 - col[d - 1] - 1); ` `    ``for` `(``int` `i = 1; i < d; i++) ` `    ``{ ` `        ``concol.Add(col[i] - col[i - 1] - 1); ` `    ``} ` ` `  `    ``int` `row_size = conrow.Count; ` `    ``int` `col_size = concol.Count; ` ` `  `    ``// To store the required answer ` `    ``int` `answer = 0; ` ` `  `    ``// Every pair of row size and column size ` `    ``// would result in an unblocked region ` `    ``for` `(``int` `i = 0; i < row_size; i++) ` `    ``{ ` `        ``for` `(``int` `j = 0; j < col_size; j++) ` `        ``{ ` `            ``int` `total = (concol[j] / K) *  ` `                        ``(conrow[i] / K); ` `            ``answer += (total); ` `        ``} ` `    ``} ` `    ``return` `answer; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `N = 8, Z = 4; ` `    ``int` `[]row = { 4, 6 }; ` `    ``int` `[]col = { 3, 8 }; ` `    ``int` `r = row.Length; ` `    ``int` `d = col.Length; ` ` `  `    ``Console.Write(subgrids(N, Z, row, col, r, d)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```6
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.